如何计算 python 中在线 pdf 的页数?

How to Count number of pages of an online pdf in python?

我正在分析 NLP 会议。我需要使用 python 从在线托管的 pdf 中提取页数。 例如 : pdf 的来源是“https://www.aclweb.org/anthology/E91-1002.pdf” 输出应该是 6.

我会scrape it and then extract the information with PyPdf2.

按照 Darjusch 的建议,使用 PyPDF2。

PdfFileReader 不采用原始字节,因此您需要创建一个 file like 对象,用 pdf 文件的字节初始化。

import PyPDF2, io, requests

response = requests.get("https://www.aclweb.org/anthology/E91-1002.pdf")
pdf_file = io.BytesIO(response.content) # response being a requests Response object
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
num_pages = pdf_reader.numPages

或一行:

num_pages = PyPDF2.PdfFileReader(io.BytesIO(response.content)).numPages

num_pages 是 6,符合预期。