Repository navigation
Expand file tree
/
Copy path04_get_spd.R
More file actions
233 lines (184 loc) · 6.2 KB
/
Copy path04_get_spd.R
File metadata and controls
233 lines (184 loc) · 6.2 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# SCRIPT: 04_get_spd.R -------------------------------------------------------
#
# Author: Anton Könneke
#
# Scrape SPD Bundestag Faction Press Releases
#
# CREATED: 2023-06-15
#
# SETUP ------------------------------------------------------------------------
pacman::p_load(rvest, tidyverse, tictac)
# For the SPD Bundestag-Faction, scraping the press releases is a little easier
# than for the other pages, as they use simple index numbers in the urls to the
# pages with the press releases. Page 162 holds the first press releases from
# 2020 so I will scrape pages 1 to 162. Note that these are dynamic indexes in
# that in the future, page 162 will hold more recent press releases as today.
# GET URLS ---------------------------------------------------------------------
get_urls <- function(pages){
lapply(pages, function(page){
# create url
url <- paste0("https://www.spdfraktion.de/artikel?page=", page)
# get html
html <- read_html(url)
# extract urls
page_urls <- html %>%
html_elements("article") %>%
html_elements("h3") %>%
html_elements("a") %>%
html_attr("href")
result <- tibble(page_number = page,
urls = page_urls)
return(result)
Sys.sleep(2)
}) %>%
bind_rows()
}
spd_urls <- get_urls(pages = c("", c(1:162)))
spd_urls <- spd_urls %>%
mutate(urls = paste0("https://www.spdfraktion.de", urls))
# SCAPRE ARTICLES --------------------------------------------------------------
get_articles <- function(urls) {
length <- length(urls)
counter <- 0
lapply(urls, function(url){
print(url)
counter <<- counter + 1
print(paste0("Scrape Article ", counter, " of ", length, sep = " "))
html <- read_html(url)
article <- list()
Sys.sleep(1)
article$headline <- html %>%
html_element("article") %>%
html_element("h2") %>%
html_text()
article$title <- html %>%
html_element("article") %>%
html_element("h1") %>%
html_text()
article$subtitle <- html %>%
html_element("article") %>%
html_element(".text-intro") %>%
html_text()
article$date <- html %>%
html_element("article") %>%
html_element("#main-image-aside") %>%
html_element("dl") %>%
html_element("dd") %>%
html_text()
article$meta <- html %>%
html_element("article") %>%
html_element("#main-image-aside") %>%
html_element("dl") %>%
html_elements("dd") %>%
html_text() %>%
list()
article$text <- html %>%
html_element("article") %>%
html_element(".maintext") %>%
html_text2()
article$url <- url
return(article)
}) %>%
bind_rows()
}
spd_articles <- get_articles(urls = spd_urls$urls)
write_csv(spd_articles, "texts/spd_articles.csv")
# PRESS STATEMENTS -------------------------------------------------------------
get_press_urls <- function(pages){
lapply(pages, function(page){
url <- paste0("https://www.spdfraktion.de/presse/pressemitteilungen?page=", page)
Sys.sleep(2)
html <- read_html(url)
# extract urls
page_urls <- html %>%
html_elements(".view-content") %>%
html_elements("h3") %>%
html_elements("a") %>%
html_attr("href")
result <- tibble(page_number = page,
urls = page_urls)
return(result)
}) %>%
bind_rows()
}
press_urls <- get_press_urls(pages = c("", c(1:41)))
press_urls <- press_urls %>%
mutate(urls = paste0("https://www.spdfraktion.de", urls))
# function to get press releases
# SCAPRE ARTICLES --------------------------------------------------------------
get_articles <- function(urls) {
length <- length(urls)
counter <- 0
lapply(urls, function(url){
print(url)
counter <<- counter + 1
print(paste0("Scrape Article ", counter, " of ", length, sep = " "))
html <- read_html(url)
article <- list()
Sys.sleep(.3)
article$title <- html %>%
html_element("article") %>%
html_element("h1") %>%
html_text()
# fulltext
article$fulltext <- html %>%
html_element("article") %>%
html_element(".maintext") %>%
html_text2()
# promt
article$prompt <- html %>%
html_element("article") %>%
html_element(".maintext") %>%
html_element("p:nth-child(1)") %>%
html_text()
# authors
article$authors <- html %>%
html_element("article") %>%
html_element(".maintext") %>%
html_elements("em") %>%
html_text() %>%
list()
article$meta <- html %>%
html_element("article") %>%
html_element("#main-image-aside") %>%
html_element("dl") %>%
html_elements("dd") %>%
html_text() %>%
list()
article$url <- url
return(article)
}) %>%
bind_rows()
}
spd_press_releases <- get_articles(press_urls$urls)
spd_press_releases <- spd_press_releases %>%
unnest_wider(meta, names_sep = "_") %>%
unnest_wider(authors, names_sep = "_")
write_csv(spd_press_releases, "texts/spd_press_releases.csv")
# add older PMs
baseurl <- "https://www.spdfraktion.de/presse/pressemitteilungen?s=&s_date%5Bdate%5D=&e_date%5Bdate%5D=&field_legislaturen=2&sort_by=created&sort_order=&items_per_page=100&page="
spd_old_press_urls <- lapply(c(0:16), function(page){
url <- paste0(baseurl, page)
html <- read_html(url)
# extract urls
page_urls <- html %>%
html_elements("body") %>%
html_elements("article") %>%
html_elements("h3") %>%
html_elements("a") %>%
html_attr("href")
Sys.sleep(2)
result <- tibble(page_number = page,
urls = page_urls)
return(result)
}) %>%
bind_rows(.id = "page")
# scrape the old press releases
spd_old_press_urls <- spd_old_press_urls %>%
mutate(urls = paste0("https://www.spdfraktion.de", urls))
spd_press_releases_old <- get_articles(spd_old_press_urls$urls)
spd_press_releases_old <- spd_press_releases_old %>%
unnest_wider(meta, names_sep = "_") %>%
unnest_wider(authors, names_sep = "_")
write_csv(spd_press_releases_old, "texts/spd_press_releases_old.csv")
saveRDS(spd_press_releases_old, "texts/spd_press_releases_old.RDS")