【问题标题】:Trying to scrape a PDF in R, my code will only scrape 6 out of 9 pages and i'm not sure why, am I missing something in my code?试图在 R 中抓取 PDF,我的代码只会抓取 9 页中的 6 页,我不知道为什么,我的代码中是否遗漏了什么?
【发布时间】:2019-05-30 23:25:28
【问题描述】:

我试图在 R 中抓取几个 PDF,PDF1 有 9 页,PDF2 有 12 页。当我运行下面的代码时,它会刮掉两个 PDF,但只能刮到第 6 页,之后什么也没有。是否有一个原因?我的代码中缺少什么?

library(tm)
read <- readPDF(engine = "xpdf", control = list(text = "-layout")) 
document <- Corpus(URISource("C:\\Users\\Goku\\Documents\\Python Scripts\\PDF Scraping\\123.pdf"), readerControl = list(reader = read))
doc <- content(document[[1]])
head(doc)

您可以在以下位置找到 pdf:https://www.scribd.com/document/396797318/123

【问题讨论】:

  • 除了尝试使用 pdftools 作为引擎看看是否可行之外,我无法给您任何其他建议。
  • 我试过了,它仍然只打印出 9 页中的 6 页。也许我的设置搞砸了?
  • 直接使用pdftools会发生什么?喜欢pdftools::pdf_text(your pdf document here)
  • 好的,打印所有 9 页,为什么可以,但在我的代码中它最多只能打印 6 页?
  • 我不知道。我没有你的pdf。我无法用我的文档在我的机器上复制您的问题。

标签: r pdf tm pdf-scraping xpdf


【解决方案1】:

我无法复制您的问题。使用您的文档,我得到 12 页以两种方式阅读文本。检查它们是否相同也会产生 true。

tm with reader pdftools:

library(tm)

read <- readPDF(engine = "pdftools", control = list(text = "-layout")) 
document <- Corpus(URISource("396797318-123.pdf"), readerControl = list(reader = read))

直接使用pdftools:

library(pdftools)

text <- pdf_text("396797318-123.pdf")

检查它们是否相同:

tm_text <- as.vector(sapply(document, as.character))
identical(text, tm_text)
[1] TRUE

str(document)
List of 1
 $ 396797318-123.pdf:List of 2
  ..$ content: chr [1:12] "            Training and Development Policy\r\n                                                     Contents\r\"| __truncated__ "1.2 As a guiding principle, all staff must be offered appropriate and relevant\r\n     development opportunitie"| __truncated__ "   • Short Term Externally Provided Training (e.g. one or two day external\r\n     courses)\r\n   • Longer Term"| __truncated__ "     are at no cost) together with periodic training bulletins informing employees of\r\n     impending courses"| __truncated__ ...
  ..$ meta   :List of 7
  .. ..$ author       : chr "leanne.cutts"
  .. ..$ datetimestamp: POSIXct[1:1], format: "2014-02-28 17:57:34"
  .. ..$ description  : chr ""
  .. ..$ heading      : chr "Training and Development Policy"
  .. ..$ id           : chr "396797318-123.pdf"
  .. ..$ language     : chr "en"
  .. ..$ origin       : chr "PDFCreator Version 1.4.3"
  .. ..- attr(*, "class")= chr "TextDocumentMeta"
  ..- attr(*, "class")= chr [1:2] "PlainTextDocument" "TextDocument"
 - attr(*, "class")= chr [1:2] "VCorpus" "Corpus"

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多