我试图从 www.speedtest.net/awards/ca/ontario 抓取数据,当我沿着某些路径走时,标准功能似乎可以工作,但其他路径却没有。我不知道为什么。
例如,如果我进入标题并查找脚本,它会起作用
library(rvest)
URL<-read_html("http://www.speedtest.net/awards/ca/ontario")
test1<-html_nodes(URL,xpath='/html/head/script[1]')
test1
这将按预期返回 {xml_nodeset (1)}。
但是如果我进入 body 并尝试类似的东西
test2<-html_nodes(URL,xpath='/html/body/script[1]')
test2
我得到 {xml_nodeset (0)}。
为什么我无法到达 body 下方的节点?
我正在尝试使用下面的代码,但我已将问题追溯到上述问题。
real<-html_nodes(URL,xpath='/html/body/div[1]/div[3]/div/div[2]/div/div[3]/div[2]/table')
real
有任何想法吗?
最佳答案
谢谢。使用 css 标签搜索,我能够想出这个方法来获得我想要的表格(右下角的表格)。
library(rvest)
URL<-read_html("http://www.speedtest.net/awards/ca/ontario")
table<-html_nodes(URL, "table")
table<-html_table(table)[[2]]
关于r - html_nodes 给出 {xml_nodeset (0)},我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37999105/