R:使用 rvest 包而不是 XML 包从 URL 获取链接

2024-01-02

我使用 XML 包来获取链接this url http://www.bvl.com.pe/includes/empresas_todas.dat.

# Parse HTML URL
v1WebParse <- htmlParse(v1URL)
# Read links and and get the quotes of the companies from the href
t1Links <- data.frame(xpathSApply(v1WebParse, '//a', xmlGetAttr, 'href'))

虽然这种方法非常有效,但我用过rvest并且解析网络的速度似乎比XML。我试过html_nodes and html_attrs但我无法让它发挥作用。


尽管我有评论,但您可以通过以下方式做到这一点rvest。请注意,我们需要在页面中读取htmlParse首先,因为该网站的内容类型设置为text/plain对于那个文件和那个扔rvest陷入眩晕。

library(rvest)
library(XML)

pg <- htmlParse("http://www.bvl.com.pe/includes/empresas_todas.dat")
pg %>% html_nodes("a") %>% html_attr("href")

##   [1] "/inf_corporativa71050_JAIME1CP1A.html" "/inf_corporativa10400_INTEGRC1.html"  
##   [3] "/inf_corporativa66100_ACESEGC1.html"   "/inf_corporativa71300_ADCOMEC1.html"  
## ...
## [273] "/inf_corporativa64801_VOLCAAC1.html"   "/inf_corporativa58501_YURABC11.html"  
## [275] "/inf_corporativa98959_ZNC.html"  

这进一步说明了rvest's XML封装基础。

UPDATE

rvest::read_html()现在可以直接处理这个问题:

pg <- read_html("http://www.bvl.com.pe/includes/empresas_todas.dat")
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

R:使用 rvest 包而不是 XML 包从 URL 获取链接 的相关文章

随机推荐