-
Notifications
You must be signed in to change notification settings - Fork 1
/
webscraping_geopolitics_fiscal.R
188 lines (122 loc) · 4.05 KB
/
webscraping_geopolitics_fiscal.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
# Paquetes -------------------------------------------------------------------------
library(tidyverse)
library(lubridate)
library(rvest)
# webscraping tools ----------------------------------------------------------------
# Function to get individuals news url --- ----
get_url <- function(url, topic) {
if(topic == "geopolitics"){
read_html(url) %>%
html_nodes(".article-title a") %>%
html_attr("href")
} else if (topic == "fiscalpolicy") {
read_html(url) %>%
html_nodes(".published+ .published h2 a") %>%
html_attr("href") %>%
unique() %>%
paste0("https://theconversation.com", .)
} else if (topic == "tradewar") {
}
}
# Function to get the text and date of the news --- ----
get_text <- function(url, topic){
# if geopolitics
if(topic == "geopolitics") {
# Reading the news page
html <- read_html(url)
# Extracting the date
date <- html %>%
html_nodes(".article-single .article-date") %>%
html_text()
# Extracting the news body
body <- html %>%
html_nodes(".article-body") %>%
html_text() %>%
str_remove_all("\n") %>%
paste(colapse = " ")
# header
header <- html %>%
html_nodes(".article-single .article-title") %>%
html_text()
# bind all elements in a dataframe
df <- data.frame(
date = date,
header = header,
text = body)
return(df)
} else if (topic == "fiscalpolicy") {
html <- read_html(url)
date <- html %>%
html_nodes("time") %>%
html_text()
header <- html %>%
html_nodes("strong") %>%
html_text() %>%
str_remove_all("\n") %>%
paste(collapse = " ")
body <- html %>%
html_nodes(".instapaper_body p") %>%
html_text() %>%
str_remove_all("\n") %>%
paste(collapse = " ")
df <- data.frame(
date = date,
header = header,
text = body
)
return(df)
}
}
# Geopolitical News webscraping ----------------------------------------------------
# url to general wb --- ---
geopolitics_url <- paste0(
"https://www.geopoliticalmonitor.com/page/",
1:235,
"/?s§ion&topic&start&end®ion&country&advanced"
)
# Extracting individual news --- ---
geopolitics_news_url <- list()
for (i in seq_along(geopolitics_url)) { # Quedó en 151
geopolitics_news_url[[i]] <- get_url(geopolitics_url[i], topic = "geopolitics")
print(i)
if(i %in% seq(50, 300, by = 50)) {saveRDS(geopolitics_news_url, "geopolitics_url.RDS")}
}
geopolitics_news_url_ <- unlist(geopolitics_news_url) %>%
unique()
# getting the news --- --- ---
geopolitics_news <- list()
for (i in 235:length(geopolitics_news_url_)) {
geopolitics_news[[i]] <- get_text(geopolitics_news_url_[i+4], "geopolitics")
if(i %in% seq(100, 3000, by = 100)) {write_rds(geopolitics_news, "geopolitics_news.RDS")}
print(i)
}
geopolitics_news <- bind_rows(geopolitics_news)
write_csv(geopolitics_news, "data/geopolitics_news.csv")
# Fiscal news webscraping ----------------------------------------------------------
# URL to general wp --- ---
fiscal_url <- paste0(
"https://theconversation.com/us/topics/fiscal-policy-973?page=",
1:3
)
fiscal_url_2 <- paste0(
"https://theconversation.com/us/search?adapter=pg3&date=all&language=en&page=",
1:10,
"&q=fiscal&sort=relevancy"
)
# Extracting individuals news url --- ---
fiscal_news_url <- list()
for (i in seq_along(fiscal_url)) {
fiscal_news_url[[i]] <- get_url(fiscal_url[i], "fiscalpolicy")
print(i)
}
fiscal_news_url <- unlist(fiscal_news_url) %>%
unique()
# getting the news --- --- ---
fiscal_news <- list()
for (i in seq_along(fiscal_news_url)) {
fiscal_news[[i]] <- get_text(fiscal_news_url[i], "fiscalpolicy")
print(i)
}
fiscal_news <- bind_rows(fiscal_news)
write_csv(fiscal_news, "fiscal_news.csv")
write_csv(geopolitics_news, "geopolitics_news.csv")