Using PDFMiner (Python) with online pdf files. Encode the url?(使用 PDFMiner (Python) 处理在线 pdf 文件.编码网址?)
问题描述
我希望使用 PDFMiner
提取在线可用的 pdf 文件的内容.
I am wishing to extract the content of pdf files available online using PDFMiner
.
我的代码基于 文档 用于提取硬盘上的PDF文件内容:
My code is based on the one available in the documentation used to extract the content of PDF files on the hard disk:
# Open a PDF file.
fp = open('mypdf.pdf', 'rb')
# Create a PDF parser object associated with the file object.
parser = PDFParser(fp)
# Create a PDF document object that stores the document structure.
document = PDFDocument(parser)
稍作改动后效果很好.
现在,我已尝试将 urllib2.openurl
用于在线 PDF,但这不起作用.我收到一条错误消息:coercing to Unicode: need string or buffer, instance found
.
Now, I have tried urllib2.openurl
for online PDFs but that doesn't work. I get an error message : coercing to Unicode: need string or buffer, instance found
.
如何从 urllib2.openurl
获取字符串(或其他),以便在我给它一个 PDF 文件名时它与 open
函数相同(相对于 URL)`?
How can I get a string (or whatever) from urllib2.openurl
so that it is the same as what the open
function when I give it a PDF file name (versus an URL)`?
如果我的问题不清楚,请告诉我.
Please tell me if my question is not clear.
推荐答案
嗯,终于找到解决方案了,
Well, I finally found out a solution,
我求助于 Request
和 StringIO
并摆脱了 open('my_file', 'rd')
命令
I resorted on Request
and StringIO
and got rid off the open('my_file', 'rd')
command
from urllib2 import Request
from StringIO import StringIO
url = 'my_url'
open = urllib2.urlopen(Request(url)).read()
memoryFile = StringIO(open)
parser = PDFParser(memoryFile)
这样 Python 将 url 视为一个文件(这么说).
That way Python considers the url as a file (to say so).
这篇关于使用 PDFMiner (Python) 处理在线 pdf 文件.编码网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:使用 PDFMiner (Python) 处理在线 pdf 文件.编码网址?


- 我如何透明地重定向一个Python导入? 2022-01-01
- 计算测试数量的Python单元测试 2022-01-01
- CTR 中的 AES 如何用于 Python 和 PyCrypto? 2022-01-01
- 检查具有纬度和经度的地理点是否在 shapefile 中 2022-01-01
- 使用 Cython 将 Python 链接到共享库 2022-01-01
- 使用公司代理使Python3.x Slack(松弛客户端) 2022-01-01
- ";find_element_by_name(';name';)";和&QOOT;FIND_ELEMENT(BY NAME,';NAME';)";之间有什么区别? 2022-01-01
- 如何使用PYSPARK从Spark获得批次行 2022-01-01
- 我如何卸载 PyTorch? 2022-01-01
- YouTube API v3 返回截断的观看记录 2022-01-01