-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathget_fdpbt.R
210 lines (171 loc) · 5.71 KB
/
get_fdpbt.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
# get fdp bundestagsfraktions press releases
pacman::p_load(rvest, tidyverse, RSelenium)
url <- "https://www.fdpbt.de/presse"
# Start a Selenium firefox browser
driver <- rsDriver(
browser = "firefox",
port = 45589L,
verbose = FALSE,
chromever = NULL
)
# extract the client for readability of the code to follow
remote_driver <- driver[["client"]]
# Navigate to the webpage
remote_driver$navigate(url)
# accept cookies
cookie_button <-
remote_driver$findElement(using = "css selector", "#edit-submit")
cookie_button$clickElement()
load_more <- function(rd) {
# scroll to end of page
rd$executeScript("window.scrollTo(0, document.body.scrollHeight);", args = list())
# Find the "Load more" button by its CSS selector and ...
load_more_button <-
rd$findElement(using = "name", "op")
# ... click it
load_more_button$clickElement()
# give the website a moment to respond
Sys.sleep(1.5)
}
load_page_completely <- function(rd) {
# load more content even if it throws an error
tryCatch({
# call load_more()
load_more(rd)
# if no error is thrown, call the load_page_completely() function again
Recall(rd)
}, error = function(e) {
# if an error is thrown return nothing / NULL
})
}
load_page_completely(remote_driver)
page_source <- remote_driver$getPageSource()
webpage <- read_html(page_source[[1]])
fdp_urls <- webpage %>%
html_element("#form-wrapper > div > ul") %>%
html_elements("li") %>%
html_elements("a") %>%
html_attr("href") %>%
tibble(url = .) %>%
filter(str_detect(url, "^\\/")) %>%
mutate(url = paste0("https://www.fdpbt.de", url))
# SCRAPEING
get_fdpbt_articles <- function(urls) {
length <- length(urls)
counter <- 0
lapply(urls, function(url) {
print(url)
html <- httr::GET(url,
httr::set_cookies(
.cookies =
c(
`_pk_id.1.513c` = "7625da8f2f7960ab.1687734520.",
`_pk_ses.1.513c` = "1",
`cookie-agreed` = "2",
`cookie-agreed-categories` = "%5B%22bundestag_media_library%22%2C%22cloudinary%22%2C%22facebook_embed%22%2C%22instagram_embed%22%2C%22matomo%22%2C%22slides%22%2C%22twitter_embed%22%2C%22youtube_embed%22%5D",
`cookie-agreed-status` = "all",
`cookie-agreed-timestamp` = "1687734519",
`cookie-agreed-version` = "unknown"
)
)) %>%
read_html()
counter <<- counter + 1
print(paste0("Scrape Article ", counter, " of ", length, sep = " "))
# is_pm_statement <-
# tryCatch({
# html %>%
# html_elements(
# "#block-uv-fdpbt-theme-content > article > div.mdb-wrapper.full > div > div.person-data2 > div"
# ) %>%
# html_text2() %>%
# tolower() %>%
# str_detect("presse|statement")
# },
# error = function(e) {
# F
# }
# )
is_no_pm_statement <- html %>%
html_elements(
"#block-uv-fdpbt-theme-content > article > div.mdb-wrapper.full > div > div.person-data2 > div"
) %>%
html_text2() %>%
tolower() %>%
str_detect("presse|statement") %>%
is_empty()
if (!is_no_pm_statement) {
article <- list()
article$url <- url
# title
article$title <- html %>%
html_element("body > div > div > main > div > div > #block-uv-fdpbt-theme-content > article > h1") %>%
html_text2()
#date
article$date <- html %>%
html_element("body > div > div > main > div > div > #block-uv-fdpbt-theme-content > article > time") %>%
html_text2()
# fulltext
article$fulltext <- html %>%
html_element("body > div > div > main > div > div > #block-uv-fdpbt-theme-content") %>%
html_elements("article:not(div) > p") %>%
html_text2() %>%
str_flatten() %>%
str_squish()
# authors
article$authors <- html %>%
html_elements(
"#block-uv-fdpbt-theme-content > article > div.mdb-wrapper.full > div > div.person-data2 > div"
) %>%
html_text2() %>%
str_remove(" Pressemitteilung")
article$type <-
ifelse(
html %>%
html_elements(
"#block-uv-fdpbt-theme-content > article > div.mdb-wrapper.full > div > div.person-data2 > div"
) %>%
html_text2() %>%
str_detect("Pressemitteilung"),
"Pressemitteilung",
ifelse(
html %>%
html_elements(
"#block-uv-fdpbt-theme-content > article > div.mdb-wrapper.full > div > div.person-data2 > div"
) %>%
html_text2() %>%
str_detect("Statement"),
"Statement",
NA
)
)
# cache
if (!file.exists("texts/cache/fdpbt_article_cache.csv")) {
write.table(
article,
file = "texts/cache/fdpbt_article_cache.csv",
row.names = F,
sep = ",",
append = F
)
} else {
write.table(
article,
file = "texts/cache/fdpbt_article_cache.csv",
row.names = F,
col.names = F,
sep = ",",
append = T
)
}
# after scraping four pages, wait
if (counter %% 5 == 0) {
Sys.sleep(1.2)
}
return(article)
} else {
print("Url is neither pm nor press statement and was skipped")
return(NULL)
}
}) %>%
bind_rows()
}