【问题标题】:Using rvest to creating a database from multiple XML files使用 rvest 从多个 XML 文件创建数据库
【发布时间】:2019-09-17 03:49:32
【问题描述】:

使用R从多个在线XML文件中提取相关数据创建数据库

我刚开始学习 R 来做文本分析。这是我正在尝试做的事情:我正在尝试使用 rvest in r 从在线 XML 文件创建第 116 届国会法案摘要的 CSV 数据库。数据库应该有两列:

  1. 账单的标题。
  2. 法案摘要文本。

网站来源是https://www.govinfo.gov/bulkdata/BILLSUM/116/hr

我遇到的问题是

我想收集从搜索返回的所有演讲。所以我需要网络抓取多个链接。但我不知道如何确保 r 运行具有一系列不同链接的功能,然后提取预期的数据。

我已经尝试了以下代码,但我不确定如何将它们准确地应用于我的具体问题。另外,我收到了我的代码的错误报告。请在下面查看我的代码。提前感谢您的帮助!

library(rvest)    
library(tidyverse)
library(purrr)
html_source <- "https://www.govinfo.gov/bulkdata/BILLSUM/116/hr?page="


map_df(1:997, function(i) {  

  cat(".")

  pg <- read_html(sprintf(html_source, i))

  data.frame(title = html_text(html_nodes(pg, "title")),
             bill_text %>% html_node("summary-text") %>% html_text(),
             stringsAsFactors = FALSE) 

}) -> Bills 

open.connection(x, "rb") 中的错误:HTTP 错误 406。

【问题讨论】:

    标签: r xml web-scraping rvest xml2


    【解决方案1】:

    在该页面的底部是一个包含所有 XML 文件的 zipfile 的链接,因此您无需单独抓取每个文件(a suggested crawl-delay of 10s 会很麻烦),您只需下载 zipfile 并解析 XML 文件使用 xml2(rvest 用于 HTML):

    library(xml2)
    library(purrr)
    
    local_dir <- "~/Downloads/BILLSUM-116-hr"
    local_zip <- paste0(local_dir, '.zip')
    
    download.file("https://www.govinfo.gov/bulkdata/BILLSUM/116/hr/BILLSUM-116-hr.zip", local_zip)
    # returns vector of paths to unzipped files
    xml_files <- unzip(local_zip, exdir = local_dir)
    
    bills <- xml_files %>%
        map(read_xml) %>% 
        map_dfr(~list(
            # note xml2 functions only take XPath selectors, not CSS ones
            title = xml_find_first(.x, '//title') %>% xml_text(),
            summary = xml_find_first(.x, '//summary-text') %>% xml_text()
        ))
    
    bills
    #> # A tibble: 1,367 x 2
    #>    title                               summary                             
    #>    <chr>                               <chr>                               
    #>  1 For the relief of certain aliens w… Provides for the relief of certain …
    #>  2 "To designate the facility of the … "Designates the facility of the Uni…
    #>  3 Consolidated Appropriations Act, 2… <p><b>Consolidated Appropriations A…
    #>  4 Financial Institution Customer Pro… <p><strong>Financial Institution Cu…
    #>  5 Zero-Baseline Budget Act of 2019    <p><b>Zero-Baseline Budget Act of 2…
    #>  6 Agriculture, Rural Development, Fo… "<p><b>Highlights: </b></p> <p>This…
    #>  7 SAFETI Act                          <p><strong>Security for the Adminis…
    #>  8 Buy a Brick, Build the Wall Act of… <p><b>Buy a Brick, Build the Wall A…
    #>  9 Inspector General Access Act of 20… <p><strong>Inspector General Access…
    #> 10 Federal CIO Authorization Act of 2… <p><b>Federal CIO Authorization Act…
    #> # … with 1,357 more rows
    

    summary 列是 HTML 格式的,但总的来说这已经很干净了。

    【讨论】:

    • 您好 Alistaire,很抱歉迟到了回复您。非常感谢你的帮助。它完美地工作!另外,我试图自己弄清楚,这与您的代码非常相似。您介意我对您的代码提出以下两个问题吗?
    • 1) 我没有看到你对“.x”的定义,所以它是指从网站提取的所有文件吗?是默认的吗? 2) XPath 选择器和 CSS 选择器的区别如何?再次感谢您的帮助!
    • 1. ~ .x 是匿名函数的a purrr shorthand,也可以写成function(x) x。那么,在这种情况下,它会遍历读入的 XML 文件,因此 .x 是单个文件。
    • 2.对于 XML,您必须使用 XPath 来选择您关心的节点。使用 HTML(通过 rvest),您可以使用 XPath 或 CSS selectors(它们没有那么强大,但更容易编写)。在这种情况下,您知道确切的节点名称,因此您可以使用 //{node name} 在 DOM 中的任何位置搜索名为 {node name} 的节点。
    • 嗨,Alistaire,非常感谢您的回复。很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 1970-01-01
    • 2011-08-22
    • 1970-01-01
    • 1970-01-01
    • 2016-08-11
    • 1970-01-01
    相关资源
    最近更新 更多