gpt4 book ai didi

python - 如何使用 Python 抓取 PDF;仅特定内容

转载 作者:行者123 更新时间:2023-12-04 16:27:54 26 4
gpt4 key购买 nike

我正在尝试从网站上提供的 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 页上的 Jade 米数据,我必须为期末库存、导出等创建单独的文件。我是 Python 新手,我不确定如何单独抓取内容。如果我能弄清楚一个月,那么我就可以创建一个循环。但是,我对如何处理一个文件感到困惑。

有人能帮我一下吗,TIA。

最佳答案

这里有一个使用 PyPDF2 的小例子,请求和 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)

关于python - 如何使用 Python 抓取 PDF;仅特定内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59130672/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com