-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex_css2.R
39 lines (34 loc) · 1.12 KB
/
ex_css2.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
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.tagesanzeiger.ch/'
# return only the headings of articles
read_html(base_url) %>%
html_elements('h3 > span > span:last-child') %>% # span:nth-child(2) doesn't work
html_text()
# return only the headings of paid articles
read_html(base_url) %>%
html_elements('h3 > span > span + span') %>%
html_text()
# return only the headings of paid articles that are accompanied with a teaser image
read_html(base_url) %>%
html_elements('figure + div h3 > span > span + span') %>%
html_text()
# bug: might also select some articles that have an "invisible" figure