我想从movie_link变量中提取所有电影ID。
movie_webpage <- read_html("http://www.imdb.com/chart/top?ref_=nv_wl_img_3")
movie_nodes <- html_nodes(movie_webpage,'.titleColumn a')
movie_link = sapply(html_attrs(movie_nodes),`[[`,'href')
movieid <- movie_webpage %>%
html_node(".titleColumn a") %>%
html_attr("href")
“ / title / tt0111161 /?pf_rd_m = A2FGELUUNOQJNL&pf_rd_p = e31d89dd-322d-4646-8962-327b42fe94b1&pf_rd_r = EE9DQ2NA8KK0XDG1CS8Q&pf_rd_s = center-1&pf_rd_t = f&c_top_p = c
结果应为:tt0111161
答案 0 :(得分:1)
相同的movie-id存储在多个位置,其中每个电影的末尾都有一个功能区,您可以从data-tconst
属性中获取movie-id,而无需太多麻烦。
library(rvest)
url <- "http://www.imdb.com/chart/top?ref_=nv_wl_img_3"
url %>%
read_html %>%
html_nodes("div.wlb_ribbon") %>%
html_attr("data-tconst")
#[1] "tt0111161" "tt0068646" "tt0071562" "tt0468569" "tt0050083" "tt0108052"
#[7] "tt0167260" "tt0110912" "tt0060196" "tt0137523" "tt0120737" "tt0109830"
#....