R、Xpath、刮擦

问题描述 投票:0回答:2

我想使用 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 问题是什么?代码有什么问题?

r web-scraping xpath
2个回答
1
投票

您将 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)

0
投票

使用 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()
© www.soinside.com 2019 - 2024. All rights reserved.