R, Xpath, 抓取
R, Xpath, Scrape
我想使用 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
问题是什么?代码有什么问题?
使用 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()
您在 xpathApply 部分中放置了 A 而不是 doc...
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)
我想使用 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 问题是什么?代码有什么问题?
使用 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()
您在 xpathApply 部分中放置了 A 而不是 doc...
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)