-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathLesson10.Rmd
299 lines (227 loc) · 8.78 KB
/
Lesson10.Rmd
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
---
params:
lesson: "Lesson 10"
title: "Scraping webpage data and text"
bookchapter_name: "Webscrape text and data"
bookchapter_section: "https://rvest.tidyverse.org/"
functions: "`read_html`,`html_node`,`html_nodes`, `html_attr`, `html_text`, `html_table`"
packages: "`rvest`,`xml2`,`magick`,`magrittr`"
# end inputs ---------------------------------------------------------------
header-includes: \usepackage{float}
always_allow_html: yes
output:
html_document:
code_folding: show
---
```{r, setup, echo = FALSE, cache = FALSE, include = FALSE}
options(width=100)
knitr::opts_chunk$set(
eval = F, # run all code
echo = TRUE, # show code chunks in output
tidy = TRUE, # make output as tidy
message = FALSE, # mask all messages
warning = FALSE, # mask all warnings
comment = "",
tidy.opts=list(width.cutoff=100), # set width of code chunks in output
size="small" # set code chunk size
)
```
\
<!-- install packages -->
```{r, load packages, eval=T, include=T, cache=F, message=F, warning=F, results='hide',echo=F}
packages <- c("ggplot2","ggthemes","dplyr","tidyverse","zoo","RColorBrewer","viridis","plyr")
if (require(packages)) {
install.packages(packages,dependencies = T)
require(packages)
# load tvthemes
devtools::install_github("Ryo-N7/tvthemes")
}
lapply(packages,library,character.only=T)
```
<!-- ____________________________________________________________________________ -->
<!-- ____________________________________________________________________________ -->
<!-- ____________________________________________________________________________ -->
<!-- start body -->
# `r paste0(params$lesson,": ",params$title)`
\
Functions for `r params$lesson`
`r params$functions`
\
Packages for `r params$lesson`
`r params$packages`
\
# Agenda
<!-- ----------------------- image --------------------------- -->
<div align="center">
<img src="https://raw.githubusercontent.com/tidyverse/rvest/main/man/figures/logo.png" style=width:25%>
</div>
Webscrape data from `R` using `rvest` and `xml2` packages.
[`r params$bookchapter_name`](`r params$bookchapter_section`).
\
# Reading webpage data (HTML)
Reading webpage data can save a ton of time and space by avoiding the need to download the data to your drive and read it into `R` manually.
Rather than pulling the entire webpage, specify HTML elements (nodes), such as headings, tables, images to scrape.
Use `html_node` to get only the first HTML node and `html_nodes` to pull all elements. This is useful when you want to grab just the first element or all data associated with that node, e.g. within a table.
```{r}
require(rvest)
require(xml2)
require(dplyr)
url <- "https://r4ds.had.co.nz/data-visualisation.html" # url to scrape
url %>% rvest::read_html() # scrape HTML text and data
# scraping the first node only for third order headings ("h3")
url %>% read_html %>% html_node("h3") # get only first section heading
# scraping all nodes in the webpage
url %>% read_html %>% html_nodes("h3") # get section headings
url %>% read_html %>% html_nodes("img") # get just images
```
Write a simple function to pull the HTML node you're interested in
```{r}
node <- function(n){
url %>% read_html %>% html_nodes(n) # n = user defined node
}
```
Pull specific text, such as headings using HTML elements, i.e. `<h1>`, `<img>`, etc
```{r}
node("h1") # get first order headings
# now pull just the text without the HTML elements
node("h2") %>% html_text(trim=T)
node("strong") %>% html_text(trim=T) # get just bold text
```
Find and pull all unique `R` functions from the webpage and view the first 10 results
```{r}
node("code") %>% html_text(trim = T) %>% unique %>% head(10)
```
\
#### If you're unsure what node to call, right click on the piece of data/text you want from the webpage and click "Inspect". Here, you can identify the HTML element you need.
E.g. I want just the quotes from the page, but don't know what type of node they belong to. After inspecting the element, we can see it's called a 'blockquote'.
<!-- ----------------------- image --------------------------- -->
<div align="center">
<img src="img/lesson7_inspect.jpg" style=width:100%>
</div>
<!-- ----------------------- image --------------------------- -->
\
```{r}
node("blockquote") %>% html_text(trim = T)
```
Scrape table data
```{r}
url <- "https://www.imdb.com/title/tt0094625/?ref_=fn_al_tt_1"
url %>% read_html %>% html_table(trim = T) # get just tables
```
## Finding and scraping specific attributes within a webpage
Let's say you wanted to scrape all the urls within a webpage, but ignore the urls contained within some parts of the page, e.g. the sidebar or footer.
We can use `html_attr()` to identify nested elements within HTML elements.
This code scrapes the urls from the below webpage, but inclues all possible urls, which is not what we want.
```{r}
url <- "https://r4ds.had.co.nz/index.html"
# first we see what type of attributes are available for urls ('a' tags)
node("a") %>%
html_attrs() %>%
head
node("a") %>% head(20) # return first 20 results
```
Specifying the main body of the webpage ("main") solves this, but we still want just the urls. Using `html_attr()` gives us just the url portion of the HTML link element (`<a>`)
```{r}
node("main") %>% html_nodes("a") %>% html_attr("href") %>% head(20) # return first 20 results
```
If you wanted just the text associated with the links and not the link themselves, just pull the text instead of using `html_attr()`
```{r}
node("main") %>% html_nodes("a") %>% html_text() %>% head(20) # return first 20 results
```
To pull a value/term from a webscraped vector, you can use `extract2()` from the `magrittr` package to specify the position in the vector
```{r}
require(magrittr)
node("main") %>%
html_nodes("a") %>%
extract2(3) %>% # get the 3rd scraped term
html_text()
```
Alternatively, you can index the term
```{r, eval = F}
require(rvest)
node("main") %>%
html_nodes("a") %>%
.[[3]] %>% # get the 3rd scraped term
html_text()
# to return a range/sequence
node("main") %>%
html_nodes("a") %>%
.[3:6] %>% # get range
html_text()
```
## Alternative method using `xml`
You can also search for nodes and attributes using xpaths (XML), if that's your thing.
The string convention is './/<string>' for each node/attribute
```{r}
require(xml)
# find all XML returning 'a' tags
url %>% read_html() %>%
xml_find_all(".//a")
# find the first XML returning 'list'
url %>% read_html() %>%
xml_find_first(".//li")
```
## Plotting images directly from webpages
Use `image_read()` from the `magick` package to plot images directly after scraping from a webpage
```{r}
require(magick)
node("img") %>% html_attr("src") %>% image_read()
```
## More functions
<!-- Add these (20200402) -->
```{r, eval=F, echo=F}
url <- "https://en.wikipedia.org/wiki/"
fh <- "Melbourne"
# use css selectors e.g. search for css class
paste0(url,fh) %>%
read_html() %>% # read webpage
html_nodes(":contains('infobox')")
paste0(url,fh) %>%
read_html() %>% # read webpage
html_nodes("*:contains('inf')") # search for wildcard string
# for extracting all divs with class 'a'
site %>%
read_html() %>%
html_nodes("div.a") %>%
html_text()
# get classes matching specific section on webpage
fc <- paste0(url,fh) %>% #
read_html() %>%
html_nodes("td") %>%
xml_attrs() %>% # get all classes that belong to above node
purrr::map("class") %>%
unlist %>% .[1:10]
fc <- paste0(".",fc) %>% # add period to class name
str_flatten(collapse = " ") # collapse into single string
# search all classes values (need to fix)
paste0(url,fh) %>%
read_html() %>% # read webpage
html_node(fc) %>% # read class
html_node("img") %>% # read img
html_attr("src") # pull src chr
```
## Challenge
Find and scrape the image from the below url and plot it, all without leaving `R` or saving the img to your drive.
```{r}
url <- "https://www.imdb.com/title/tt0094625/?ref_=fn_al_tt_1"
```
```{r,echo=F,eval=T,warning=F}
require(magick)
require(magrittr)
url <- "https://www.imdb.com/title/tt0094625/?ref_=fn_al_tt_1"
node("img") %>% html_attr("src") %>% extract2(3) %>% image_read()
```
<!-- ## Bypassing paywalls -->
```{r, eval = F, echo = F}
require(rvest)
# save html structure to separate webpage
url <- "https://www.businessinsider.com/how-to-turn-free-newsletter-newsette-into-a-profitable-business-2020-10?utm_source=ebizfacts&utm_medium=sendy&utm_campaign=newsletter&r=AU&IR=T"
url %>% read_html() %>%
html_node("body") %>%
write_xml("/Users/malishev/Desktop/webscrape1.html")
# write html paragraph text to file
url %>% read_html() %>%
html_nodes("p") %>%
html_text(trim=T) %>%
write_lines("/Users/malishev/Desktop/webscrape1.txt")
```