问题描述
我希望使用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)
通过一些小的改动,效果很好.
That works quite well with some small changes.
现在,我已经尝试过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
中获取字符串(或其他内容),以使其与open
函数使用的PDF文件名(而不是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文件一起使用.编码网址?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!