【问题标题】:rvest html_tablervest html_table
【发布时间】:2015-10-15 09:59:34
【问题描述】:

我正在尝试使用 html_tablervest 包提取表

library(rvest)
test <- html("http://www.privacyrights.org/data-breach/new?title=")
test %>% html_table(html_nodes("table.data-breach-table")[[1]])

但是,我不断收到错误

UseMethod("html_nodes") 中的错误:没有适用的方法 'html_nodes' 应用于“字符”类的对象

【问题讨论】:

    标签: r html-table rvest


    【解决方案1】:

    如果你无论如何都要嵌套带括号的调用,为什么还要麻烦管道呢?

    html_table(html_nodes(test, "table.data-breach-table")[[1]])
    

    否则,请使用完整的管道并使用magrittr

    library(magrittr)
    
    test %>% 
      html_nodes("table.data-breach-table") %>% 
      extract2(1) %>%
      html_table()
    

    注意:

    • 您使用的 URL 没有您想要的表格
    • 您应该使用最新的rvestread_html

    至于它为什么不工作,你错误地使用管道 test 并且 html_nodes 是在 table… 字符串而不是它预期的解析 HTML 文档上运行。

    由于您正在尝试清除漏洞,这可能会有所帮助:

    library(rvest)
    library(dplyr)
    library(pbapply)
    
    urls <- sprintf("http://www.privacyrights.org/data-breach?title=&page=%d", 1:94)
    
    pblapply(urls, function(URL) {
    
      pg <- read_html(URL)
    
      tab <- html_nodes(pg, "table")[3]
      rows <- html_nodes(tab, "tr:not(.data-breach-bottom)")
    
      bind_rows(lapply(seq(2, length(rows)-2, by=2), function(i) {
    
        tds_1 <- html_nodes(rows[i], "td")
        tds_2 <- html_text(html_nodes(rows[i+1], "td"), trim=TRUE)
    
        data_frame(date_public=html_text(tds_1[1], TRUE),
                   name_loc=html_text(tds_1[2], TRUE),
                   entity=html_text(tds_1[3], TRUE),
                   type=html_text(tds_1[4], TRUE),
                   recs=html_text(tds_1[5], TRUE),
                   descr=tds_2[1])
    
      }))
    
    }) -> things
    

    它来自我的一个老gitst。如果您确实计划清除所有违规行为,则需要在其中添加随机睡眠延迟。

    另请注意,它是扭曲的数据,并且在您尝试使用它时要非常清楚它的局限性(我以数据泄露研究为生)。

    【讨论】:

    • 非常感谢,我会对您推荐的任何其他数据泄露来源感兴趣
    • 我实际上建议加入Society of Information Risk Analysts(它是免费的)并浏览档案(很多链接)。还有 。不过,这在很大程度上取决于您要完成的工作。
    • 嗨,我快速运行代码只是为了尝试了解它是如何工作的。我对 R 很陌生,所以在软件上有点挣扎 - 它给了我一个错误错误:期望一个外部指针调用自:eval(expr,envir,enclos)
    • 还有一个 not-so-secret URL 提供所有但最近的违规行为的 CSV(这是一个丑陋的 CSV)
    • 由于格式更改导致抓取中断。代码块现在可以工作了。
    猜你喜欢
    • 1970-01-01
    • 2016-09-14
    • 2018-08-25
    • 1970-01-01
    • 1970-01-01
    • 2017-06-26
    • 2017-07-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多