Lấy toán bộ node trong thẻ strong là thẻ span:
library(rvest)
lego_movie <- read_html("http://www.imdb.com/title/tt1490017/")
rating <- lego_movie %>%
html_nodes("strong span") %>%
html_text() %>%
as.numeric()
rating
## [1] 7.8
Lấy toàn bộ node là thẻ img nằm trong class=‘poster’:
poster <- lego_movie %>%
html_nodes(".poster img") %>%
html_attr("src")
poster
## [1] "https://ia.media-imdb.com/images/M/MV5BMTg4MDk1ODExN15BMl5BanBnXkFtZTgwNzIyNjg3MDE@._V1_UX182_CR0,0,182,268_AL_.jpg"
Lấy node có id=‘titleCast’, class=‘itemprop’ và tên thẻ là span:
cast <- lego_movie %>%
html_nodes("#titleCast .itemprop span") %>%
html_text()
cast
## [1] "Will Arnett" "Elizabeth Banks" "Craig Berry"
## [4] "Alison Brie" "David Burrows" "Anthony Daniels"
## [7] "Charlie Day" "Amanda Farinos" "Keith Ferguson"
## [10] "Will Ferrell" "Will Forte" "Dave Franco"
## [13] "Morgan Freeman" "Todd Hansen" "Jonah Hill"
Cú pháp chung của selector nodes:
‘#titleCast <=> id=’titleCast’
.itemprop <=> class = itemprop
span <=> chọn toàn bộ các thẻ là span
Và rất rất nhiều các trường hợp khác. Cụ thể có thể xem ở link sau: