【问题标题】:rvest returning empty listrvest 返回空列表
【发布时间】:2018-07-03 20:09:48
【问题描述】:

我正在尝试通过复制 html 代码的 xpath 并使用 rvest 包来从网站上导入表格。我之前已经成功完成了多次,但是当我现在尝试它时,我只是产生了一个空列表。为了诊断我的问题,我运行了以下代码(取自https://www.r-bloggers.com/using-rvest-to-scrape-an-html-table/)。但是,这段代码也会为我生成一个空列表。

提前感谢您的帮助!

library(rvest)
url <- "http://en.wikipedia.org/wiki/List_of_U.S._states_and_territories_by_population"
population <- url %>%
  read_html() %>%
  html_nodes(xpath='//*[@id="mw-content-text"]/table[1]') %>%
  html_table()

【问题讨论】:

  • 不使用 xpath 尝试使用 css 选择器:url %&gt;% read_html() %&gt;% html_nodes(page, 'table')。这将返回页面上的表格列表,相信您对第三个表格感兴趣。您可以将列表返回或将其链接到html_table() 函数

标签: html r web-scraping rvest


【解决方案1】:

您的 xpath 查询错误。该表不是 id 为 mw-content-text 的节点的直接子节点。虽然是后代。试试

html_nodes(xpath='//*[@id="mw-content-text"]//table[1]') 

网页抓取是一项非常脆弱的工作,当网站更改其 HTML 时很容易中断。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-13
    相关资源
    最近更新 更多