-
Notifications
You must be signed in to change notification settings - Fork 0
/
demo_css1.R
49 lines (41 loc) · 1.26 KB
/
demo_css1.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
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)
}
base_url <- 'https://www.srf.ch/'
# extract article titles
read_html(base_url) %>%
html_elements('.teaser__title') %>%
html_text()
# extract lead texts
read_html(base_url) %>%
html_elements('.teaser__lead') %>%
html_text()
# extract both (with a so-called selector list)
read_html(base_url) %>%
html_elements('.teaser__title, .teaser__lead') %>%
html_text()
# extract the urls of the top navigation (descendant combinator)
read_html(base_url) %>%
html_elements('nav.masthead__portals a') %>%
html_attr('href')
# extract the text of the last element of the footer navigation (descendant combinator with pseudo-class)
read_html(base_url) %>%
html_elements('ul.footer__navigation li:last-child') %>%
html_text()