我想使用 Xpath 引用和 R 抓取一个网站。 我对此很陌生,但据我所知,我编写了以下代码,,
A <- "http://www.strompreis.elcom.admin.ch/ShowCat.aspx?placeNumber=5661&OpID=2&Period=2015"
doc <- htmlParse(A)
A <- xpathApply(A,path="//tr[1]/td/span",fun=xmlAttrs)
但是,我收到以下错误,
Error in UseMethod("xpathApply") :
no applicable method for 'xpathApply' applied to an object of class "character"
我对缩放以下 xpath 感兴趣: //tr[1]/td/span 问题是什么?代码有什么问题?
您将 A 而不是 doc 放在 xpathApply 部分...
A="http://www.strompreis.elcom.admin.ch/ShowCat.aspx?placeNumber=5661&OpID=2&Period=2015"
doc <- htmlParse(A)
xpathApply(doc,path="//tr[1]/td/span",fun=xmlAttrs)
使用 rvest,这似乎有效:
library(rvest)
A="http://www.strompreis.elcom.admin.ch/ShowCat.aspx?placeNumber=5661&OpID=2&Period=2015"
A %>% html() %>% html_nodes(xpath="//tr[1]/td/span") %>% html_text()