如何使用 Python 抓取 PDF;仅具体内容

Cam*_*lia 5 python scrapy pdf-scraping web-scraping tabula

我正在尝试从网站上提供的 PDF 中获取数据

https://usda.library.cornell.edu/concern/publications/3t945q76s?locale=en

例如,如果我查看 2019 年 11 月的报告

https://downloads.usda.library.cornell.edu/usda-esmis/files/3t945q76s/dz011445t/mg74r196p/latest.pdf

我需要第 12 页上的玉米数据,我必须为期末库存、出口等创建单独的文件。我是 Python 新手,我不知道如何单独抓取内容。如果我能用一个月的时间弄清楚,那么我就可以创建一个循环。但是,我对如何继续处理一个文件感到困惑。

有人可以帮我吗,TIA。

Gio*_*zar 7

这里有一个使用 PyPDF2、requests 和 BeautifulSoup 的小例子...请检查注释注释,这是第一个块...如果您需要更多,则需要更改 url 变量中的值

# You need install :
# pip install PyPDF2 - > Read and parse your content pdf
# pip install requests - > request for get the pdf
# pip install BeautifulSoup - > for parse the html and find all url hrf with ".pdf" final
from PyPDF2 import PdfFileReader
import requests
import io
from bs4 import BeautifulSoup

url=requests.get('https://usda.library.cornell.edu/concern/publications/3t945q76s?locale=en#release-items')
soup = BeautifulSoup(url.content,"lxml")

for a in soup.find_all('a', href=True):
    mystr= a['href']
    if(mystr[-4:]=='.pdf'):
        print ("url with pdf final:", a['href'])
        urlpdf = a['href']
        response = requests.get(urlpdf)
        with io.BytesIO(response.content) as f:
            pdf = PdfFileReader(f)
            information = pdf.getDocumentInfo()
            number_of_pages = pdf.getNumPages()
            txt = f"""
            Author: {information.author}
            Creator: {information.creator}
            Producer: {information.producer}
            Subject: {information.subject}
            Title: {information.title}
            Number of pages: {number_of_pages}
            """
            # Here the metadata of your pdf
            print(txt)
            # numpage for the number page
            numpage=20
            page = pdf.getPage(numpage)
            page_content = page.extractText()
            # print the content in the page 20            
            print(page_content)
Run Code Online (Sandbox Code Playgroud)


mic*_*z13 1

如果您需要从网站上抓取数据,我会推荐 Beautiful Soup,但看起来您将需要 OCR 来从 PDF 中提取数据。有一种东西叫做 pytesseract。看看它和教程,你应该就可以了。