【问题标题】:Extracting text from PDF url file with Python使用 Python 从 PDF url 文件中提取文本
【发布时间】:2020-11-24 12:37:12
【问题描述】:

我想从一个网站上的 PDF 文件中提取文本。 该网站包含指向 PDF 文档的链接,但是当我单击该链接时,它会自动下载该文件。是否可以从该文件中提取文本而不下载它

import fitz  # this is pymupdf lib for text extraction
from bs4 import BeautifulSoup
import requests
from io import StringIO

url = "https://www.blv.admin.ch/blv/de/home/lebensmittel-und-ernaehrung/publikationen-und-forschung/statistik-und-berichte-lebensmittelsicherheit.html"

headers = {'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36'}


response = requests.get(url, headers=headers)
soup = BeautifulSoup(response.content, 'html.parser')

all_news = soup.select("div.mod.mod-download a")[0]
pdf = "https://www.blv.admin.ch"+all_news["href"]

#https://www.blv.admin.ch/dam/blv/de/dokumente/lebensmittel-und-ernaehrung/publikationen-forschung/jahresbericht-2017-2019-oew-rr-rasff.pdf.download.pdf/Jahresbericht_2017-2019_DE.pdf

这是从 pdf 中提取文本的代码。下载文件时效果很好:

my_pdf_doc = fitz.open(pdf)
text = ""
for page in my_pdf_doc:
    text += page.getText()

print(text)

同样的问题是如果链接不自动下载pdf文件,例如这个链接:

"https://amsoldingen.ch/images/files/Bekanntgabe-Stimmausschuss-13.12.2020.pdf"

如何从该文件中提取文本

我也试过这个:

pdf_content = requests.get(pdf)
print(type(pdf_content.content))

file = StringIO() 
print(file.write(pdf_content.content.decode("utf-32")))

但我得到错误:

Traceback (most recent call last):
  File "/Users/aleksandardevedzic/Desktop/pdf extraction scrapping.py", line 25, in <module>
    print(file.write(pdf_content.content.decode("utf-32")))
UnicodeDecodeError: 'utf-32-le' codec can't decode bytes in position 0-3: code point not in range(0x110000)

【问题讨论】:

  • 您可以使用 BytesIO 下载到内存中的空间:stackoverflow.com/questions/22340265/…
  • 这对我不起作用,它给了我错误/
  • 你能告诉我如何在我的代码上应用它,也许我做错了什么

标签: python pdf beautifulsoup


【解决方案1】:

PyMuPDF 允许我们直接打开一个 BytesIO 流,如 documentation 中所述。

import requests
import fitz
import io

url = "your-url.pdf"
request = requests.get(url)
filestream = io.BytesIO(request.content)
pdf = fitz.open(stream=filestream, filetype="pdf")

pdf 然后可以像常规 PyMuPDF 文档一样被解析,如 here 所示。

附:这是我对 Stack Overflow 的第一次回答,欢迎提出任何改进/建议。

【讨论】:

    【解决方案2】:

    这是一个使用 PyPDF2 的示例。

    安装

    pip install PyPDF2

    import requests, PyPDF2
    from io import BytesIO
    
    url = 'https://www.blv.admin.ch/dam/blv/de/dokumente/lebensmittel-und-ernaehrung/publikationen-forschung/jahresbericht-2017-2019-oew-rr-rasff.pdf.download.pdf/Jahresbericht_2017-2019_DE.pdf'
    response = requests.get(url)
    my_raw_data = response.content
    
    with BytesIO(my_raw_data) as data:
        read_pdf = PyPDF2.PdfFileReader(data)
    
        for page in range(read_pdf.getNumPages()):
            print(read_pdf.getPage(page).extractText())
    

    输出:

    ' 1/21  Fad \nŒ 24.08.2020\n      Bericht 2017\n Œ 2019: Öffentliche Warnungen, \nRückrufe und Schnellwarnsystem RASFF\n      '
    

    【讨论】:

    • 刚刚添加了循环
    • 问题是我得到的结果到处都是,没有组织
    • 问题超出了范围。第一个问题得到了解答(如何在不下载的情况下阅读 pdf 文件)。现在我建议您通过访问此库的文档来学习如何阅读 pdf:pythonhosted.org/PyPDF2
    猜你喜欢
    • 2014-12-17
    • 2023-04-04
    • 2012-12-30
    • 1970-01-01
    • 1970-01-01
    • 2011-04-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多