我正在尝试使用 rvest
来学习使用 R 进行网络抓取。我正在尝试为页面的其他几个部分复制 Lego 示例并使用 selector gadget
到 id。
我从 R Studio tutorial 中提取了示例.使用下面的代码,1 和 2 有效,但 3 无效。
library(rvest)
lego_movie <- html("http://www.imdb.com/title/tt1490017/")
# 1 - Get rating
lego_movie %>%
html_node("strong span") %>%
html_text() %>%
as.numeric()
# 2 - Grab actor names
lego_movie %>%
html_nodes("#titleCast .itemprop span") %>%
html_text()
# 3 - Get Meta Score
lego_movie %>%
html_node(".star-box-details a:nth-child(4)") %>%
html_text() %>%
as.numeric()
最佳答案
我并没有真正跟上所有管道和相关代码的速度,所以可能有一些新的狂热工具可以做到这一点......但考虑到上面的答案让你到达“83/100”
,你可以这样做:
as.numeric(unlist(strsplit("83/100", "/")))[1]
[1] 83
我猜管道看起来像这样:
lego_movie %>%
html_node(".star-box-details a:nth-child(4)") %>%
html_text(trim=TRUE) %>%
strsplit(., "/") %>%
unlist(.) %>%
as.numeric(.) %>%
head(., 1)
[1] 83
或者如 Frank 所建议的,您可以使用类似以下内容计算表达式 "83/100"
:
lego_movie %>%
html_node(".star-box-details a:nth-child(4)") %>%
html_text(trim=TRUE) %>%
parse(text = .) %>%
eval(.)
[1] 0.83
关于r - 使用 R 和 rvest 进行网页抓取,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30954686/