-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path02_get_afd.R
136 lines (111 loc) · 3.7 KB
/
02_get_afd.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
# SCRIPT: 02_get_afd.R -------------------------------------------------------
#
# Author: Anton Könneke
#
# Scrape AfD Party Press Releases
#
# CREATED: 2023-06-05
#
# SETUP ------------------------------------------------------------------------
pacman::p_load(rvest, tidyverse, RSelenium)
# SCRAPE INTERACTIVE AFD PAGE --------------------------------------------------
# Instructions taken from Tim Tiefenbach Tutorial
# https://tim-tiefenbach.de/post/2023-web-scraping/
# Start a Selenium firefox browser
driver <- rsDriver(browser = "firefox",
port = 4555L,
verbose = FALSE,
chromever = NULL)
# extract the client for readability of the code to follow
remote_driver <- driver[["client"]]
# Set URL
url <- "https://www.afd.de/presse/"
# Navigate to the webpage
remote_driver$navigate(url)
# accept cookies
cookie_button <- remote_driver$findElement(using = "css selector", "p._brlbs-refuse-btn > a:nth-child(1)")
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 = "css selector", ".fusion-load-more-button")
# ... click it
load_more_button$clickElement()
# give the website a moment to respond
Sys.sleep(3)
}
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)
# save html
page_source <- remote_driver$getPageSource()
webpage <- read_html(page_source[[1]])
afd_posts <- webpage %>%
html_elements("article") %>%
html_elements("h2 > a") %>%
html_attr("href")
# SCRAPE ARTICLES -------------------------------------------------------------
articles <- list()
html <- read_html(afd_posts[[1]])
articles$headline <- html %>%
html_element("main") %>%
html_element("h2") %>%
html_text()
# atricles$date <- html %>%
# html_element("main") %>%
# html_element(".post-content") %>%
# html_elements("p") %>%
# .[[1]] %>%
# html_text()
articles$text <- html %>%
html_element("main") %>%
html_element(".post-content") %>%
html_elements("p") %>%
html_text()
get_afd_articles <- function(urls) {
articles <- lapply(urls, function(url){
html <- read_html(url)
article <- list()
article$url <- url
article$headline <- html %>%
html_element("main") %>%
html_element("h2") %>%
html_text()
article$text <- html %>%
html_element("main") %>%
html_element(".post-content") %>%
html_elements("p") %>%
html_text()
return(article)
Sys.sleep(3+runif(1))
})
return(articles)
}
# GET FULLTEXT -----------------------------------------------------------------
stop("this will start a time-intensive scrape")
afd_articles <- get_afd_articles(afd_posts)
# save to disk
saveRDS(afd_articles, "texts/afd_articles.RDS")
afd_articles <- readRDS("texts/afd_articles.RDS")
afd_articles <- afd_articles %>%
tibble() %>%
unnest_wider(1) %>%
unnest_wider(3, names_sep = "_") %>%
mutate(date = str_extract(text_1, "\\d+\\.\\s\\w*\\s\\d{4}"),
author = str_extract(headline, regex("^.*(?=\\:)")),
author = str_remove(author, "\\:.*")) %>%
select(!text_1) %>%
rowwise() %>%
mutate(fulltext = paste(c_across(starts_with("text")), collapse = "\n")) %>%
select(!starts_with("text"))
write_csv(afd_articles, "texts/afd_articles.csv")