【问题标题】:How to get the link inside html_table using rvest?如何使用 rvest 获取 html_table 中的链接?
【发布时间】:2017-06-26 11:14:08
【问题描述】:
library("rvest")
url <- "myurl.com"
tables<- url %>%
      read_html() %>%
      html_nodes(xpath='//*[@id="pageContainer"]/table[1]') %>%
      html_table(fill = T)
tables[[1]]

单元格的html内容是这样的

<td><a href="http://somelink.com" target="_blank">Click Here</a></td>

但在抓取的 html 中我只得到 ,

点击这里

【问题讨论】:

  • 您对所有单个单元格的 href 满意吗?或者您是否特别想要 data.frame 格式的 href?因为只收集 href 属性应该很容易:%&gt;% html_nodes( "appropriate xpath or selector") %&gt;% html_attr("href");
  • XML::getHTMLLinks(url, xpQuery = "//*[@id="pageContainer"]/table[1]//@href") 应该是你所需要的全部

标签: r rvest


【解决方案1】:

您可以通过编辑 rvest::html_tabletrace 来处理此问题。

现有行为示例:

library(rvest)
x <- "https://en.wikipedia.org/wiki/Academy_Award_for_Best_Picture" %>% 
  read_html() %>% 
  html_nodes("#mw-content-text > table:nth-child(55)")

html_table(x)
#[[1]]
#                         Film   Production company(s)                         Producer(s)
#1          The Great Ziegfeld     Metro-Goldwyn-Mayer                      Hunt Stromberg
#2             Anthony Adverse            Warner Bros.                        Henry Blanke
#3                   Dodsworth Goldwyn, United Artists  Samuel Goldwyn and Merritt Hulbert
#4                Libeled Lady     Metro-Goldwyn-Mayer                 Lawrence Weingarten
#5      Mr. Deeds Goes to Town                Columbia                         Frank Capra
#6            Romeo and Juliet     Metro-Goldwyn-Mayer                     Irving Thalberg
#7               San Francisco     Metro-Goldwyn-Mayer   John Emerson and Bernard H. Hyman
#8  The Story of Louis Pasteur            Warner Bros.                        Henry Blanke
#9        A Tale of Two Cities     Metro-Goldwyn-Mayer                   David O. Selznick
#10          Three Smart Girls               Universal Joe Pasternak and Charles R. Rogers

html_table 本质上是提取 html 表格的单元格并在它们上运行 html_text。我们需要做的就是通过从每个单元格中提取 &lt;a&gt; 标记并运行 html_attr(., "href") 来替换它。

trace(rvest:::html_table.xml_node, quote({ 
  values      <- lapply(lapply(cells, html_node, "a"), html_attr, name = "href")
  values[[1]] <- html_text(cells[[1]])
}), at = 14)

新行为:

html_table(x)
#Tracing html_table.xml_node(X[[i]], ...) step 14 
#[[1]]
#                                     Film Production company(s)                    Producer(s)
#1                /wiki/The_Great_Ziegfeld                    NA           /wiki/Hunt_Stromberg
#2                   /wiki/Anthony_Adverse                    NA             /wiki/Henry_Blanke
#3                  /wiki/Dodsworth_(film)                    NA           /wiki/Samuel_Goldwyn
#4                      /wiki/Libeled_Lady                    NA      /wiki/Lawrence_Weingarten
#5            /wiki/Mr._Deeds_Goes_to_Town                    NA              /wiki/Frank_Capra
#6      /wiki/Romeo_and_Juliet_(1936_film)                    NA          /wiki/Irving_Thalberg
#7         /wiki/San_Francisco_(1936_film)                    NA /wiki/John_Emerson_(filmmaker)
#8        /wiki/The_Story_of_Louis_Pasteur                    NA             /wiki/Henry_Blanke
#9  /wiki/A_Tale_of_Two_Cities_(1935_film)                    NA        /wiki/David_O._Selznick
#10                /wiki/Three_Smart_Girls                    NA            /wiki/Joe_Pasternak

【讨论】:

  • 这似乎可以在单元格内获取单个链接。如何修改它以获取单元格中的所有链接?
  • 运行trace 代码返回以下错误:Error in fBody[[i]] : subscript out of bounds
【解决方案2】:

如果您想获取“href”标签的值,请使用:

//*[@id="pageContainer"]/table[1]//@href

我在http://xpather.com/RtnrY9fh(xpath 在线)上对此进行了测试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-07-19
    • 2018-08-25
    • 2023-03-12
    • 1970-01-01
    • 2017-07-20
    • 2016-05-16
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多