-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_raw_html.R
65 lines (52 loc) · 1.52 KB
/
demo_raw_html.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
if(!require(dplyr)) {
install.packages("dplyr", repos="http://cran.us.r-project.org")
require(dplyr)
}
if(!require(rvest)) {
install.packages("rvest", repos="http://cran.us.r-project.org")
require(rvest)
}
if(!require(httr)) {
install.packages("httr", repos="http://cran.us.r-project.org")
require(httr)
}
if(!require(purrr)) {
install.packages("purrr", repos="http://cran.us.r-project.org")
require(purrr)
}
if(!require(jsonlite)) {
install.packages("jsonlite", repos="http://cran.us.r-project.org")
require(jsonlite)
}
# the httr way
base_url <- 'https://timogrossenbacher.ch'
response <- httr::GET(base_url)
response
# extract content (response body)
httr::content(response)
# the rvest way
rvest::read_html('https://asvz.ch')
# extract all the text from the website
read_html(base_url) %>%
html_text()
# base R
html_text(read_html(base_url))
# extract all the links on the page
read_html(base_url) %>%
html_elements('a') %>%
html_attr('href')
# btw: base R
html_text(html_elements(read_html(base_url), 'a'), trim = TRUE)
read_html(base_url) %>%
html_elements('a') %>%
html_text(trim=TRUE)
# extract all the links in a h1 element
read_html(base_url) %>%
# applies the html_elements function to the HTML document (root node)
html_elements('h1') %>%
# applies the html_elements function to every h1 node
html_elements('a') %>%
# applies the html_text function to every "a" node within a h1 node
html_text(trim=TRUE)
# direct html parsing
read_html('<html><body><p>Hello!</p></body></html>')