Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R:使用rvest包而不是XML包从URL获取链接_Xml_R_Web Scraping_Rvest - Fatal编程技术网

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

R:使用rvest包而不是XML包从URL获取链接,xml,r,web-scraping,rvest,Xml,R,Web Scraping,Rvest,我使用XML包从中获取链接 #解析HTML URL v1webpasse我知道您正在寻找rvest答案,但这里有另一种使用XML包的方法,它可能比您现在所做的更有效 您是否在示例(htmlParse)中看到了getLinks()函数?我使用示例中的这个修改版本来获取href链接。这是一个处理函数,因此我们可以在读取值时收集这些值,从而节省内存并提高效率 links <- function(URL) { getLinks <- function() { lin

我使用XML包从中获取链接

#解析HTML URL

v1webpasse我知道您正在寻找
rvest
答案,但这里有另一种使用
XML
包的方法,它可能比您现在所做的更有效

您是否在
示例(htmlParse)
中看到了
getLinks()
函数?我使用示例中的这个修改版本来获取
href
链接。这是一个处理函数,因此我们可以在读取值时收集这些值,从而节省内存并提高效率

links <- function(URL) 
{
    getLinks <- function() {
        links <- character()
        list(a = function(node, ...) {
                links <<- c(links, xmlGetAttr(node, "href"))
                node
             },
             links = function() links)
        }
    h1 <- getLinks()
    htmlTreeParse(URL, handlers = h1)
    h1$links()
}

links("http://www.bvl.com.pe/includes/empresas_todas.dat")
#  [1] "/inf_corporativa71050_JAIME1CP1A.html"
#  [2] "/inf_corporativa10400_INTEGRC1.html"  
#  [3] "/inf_corporativa66100_ACESEGC1.html"  
#  [4] "/inf_corporativa71300_ADCOMEC1.html"  
#  [5] "/inf_corporativa10250_HABITAC1.html"  
#  [6] "/inf_corporativa77900_PARAMOC1.html"  
#  [7] "/inf_corporativa77935_PUCALAC1.html"  
#  [8] "/inf_corporativa77600_LAREDOC1.html"  
#  [9] "/inf_corporativa21000_AIBC1.html"     
#  ...
#  ...

links尽管有我的评论,下面是使用
rvest
的方法。请注意,我们需要首先使用
htmlpasse
阅读页面,因为该站点将该文件的内容类型设置为
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"  

Richard的答案适用于HTTP页面,但不适用于我需要的HTTPS页面(维基百科)。我替换了RCurl的getURL函数,如下所示:

library(RCurl)

links <- function(URL) 
{
  getLinks <- function() {
    links <- character()
    list(a = function(node, ...) {
      links <<- c(links, xmlGetAttr(node, "href"))
      node
    },
    links = function() links)
  }
  h1 <- getLinks()
  xData <- getURL(URL)
   htmlTreeParse(xData, handlers = h1)
  h1$links()
}
库(RCurl)

链接非常有帮助,我没有检查
htmlpasse
中的示例,但是我根据您的建议修改了我的代码。在这种情况下,
XML
非常有效,但从中获取历史价格所需的时间比
rvest
更长。价格?你的问题是,你试图从网站上获取链接,我试图从网站上获取所有链接,而在上,我试图解析一个包含SIDERC1报价历史价格的表。我在两个站点上都使用了
XML
,但在后者上只能使用
rvest
rvest
使用
XML
包进行节点提取。它真的不应该更快。你是对的,因为节点提取
rvest
使用
XML
。我将在聊天中讨论我使用这些软件包的站点的时间差异。感谢您的回复。选项1似乎不再适用于当前版本的RCurl。
pg <- read_html("http://www.bvl.com.pe/includes/empresas_todas.dat")
# Option 1
library(RCurl)
getHTMLLinks('http://www.bvl.com.pe/includes/empresas_todas.dat')

# Option 2
library(rvest)
library(pipeR) # %>>% will be faster than %>%
html("http://www.bvl.com.pe/includes/empresas_todas.dat")%>>% html_nodes("a") %>>% html_attr("href")
library(RCurl)

links <- function(URL) 
{
  getLinks <- function() {
    links <- character()
    list(a = function(node, ...) {
      links <<- c(links, xmlGetAttr(node, "href"))
      node
    },
    links = function() links)
  }
  h1 <- getLinks()
  xData <- getURL(URL)
   htmlTreeParse(xData, handlers = h1)
  h1$links()
}