我希望使用 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 文件.编码网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!