【问题标题】:trying to scrape from long PDF with different table formats尝试从具有不同表格格式的长 PDF 中抓取
【发布时间】:2021-07-23 04:34:04
【问题描述】:

我正在尝试从此处提供的 276 页 PDF 中抓取:https://www.acf.hhs.gov/sites/default/files/documents/ocse/fy_2018_annual_report.pdf

文档不仅很长,而且还有不同格式的表格。我尝试使用 tabulizer 库中的 extract_tables() 函数。这成功地抓取了从文档第 143 页开始的数据表,但不适用于第 18-75 页上的表。这些页面是不可擦除的吗?如果有,为什么?

我收到错误消息,提示“列多于列名”和“不允许重复的 'row.names'”

child_support_scrape <- extract_tables(
  file   = "C:/Users/Jenny/Downloads/OCSE_2018_annual_report.pdf", 
  method = "decide", 
  output = "data.frame")

【问题讨论】:

    标签: r pdf data-extraction pdf-scraping tabulizer


    【解决方案1】:

    由于 pdf 文件中的文本不是以纯文本格式存储的。通常很难从 pdf 文件中提取文本。以下方法提供了一种从 pdf 中提取表格的替代方法。它需要pdftoolsplyr 包。

    # Download the pdf file as a variable in R
    pdf_text <- pdftools::pdf_text("https://www.acf.hhs.gov/sites/default/files/documents/ocse/fy_2018_annual_report.pdf")
    
    # Focus on the table in page 22
    pdf_text22 <- strsplit(pdf_text[[22]], "\n")[[1]]
    
    # Reformat the table using "regular expression"
    pdf_text22 <- strsplit(pdf_text22, " {2,100}")
    
    # Convert the table in a data frame
    pdf_text22 <- plyr::rbind.fill(lapply(pdf_text22, function(x) as.data.frame(t(matrix(x)))))
    

    可能需要额外的格式来美化数据框。

    【讨论】:

    • 感谢您对 s20012303 的帮助。您的解决方案有效。数据需要稍微清理一下,但至少我和我的电脑都可以读取它。
    猜你喜欢
    • 2022-11-30
    • 2016-12-05
    • 2020-05-01
    • 1970-01-01
    • 2021-04-15
    • 1970-01-01
    • 2020-11-12
    • 2018-12-19
    • 1970-01-01
    相关资源
    最近更新 更多