我希望提取可在网上使用PDFMiner
的pdf文件的内容。
我的代码基于用于提取硬盘上PDF文件内容的文档中可用的代码:
# 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
。
我如何从urllib2.openurl
获得一个字符串(或其他任何东西),以便当我给它一个PDF文件名(相对于URL)`时,它与open
函数相同?
如果我的问题不清楚,请告诉我。
发布于 2014-03-15 21:36:06
我终于找到了解决办法,
我求助于Request
和StringIO
,去掉了open('my_file', 'rd')
命令
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视为一个文件(可以这么说)。
https://stackoverflow.com/questions/22429193
复制相似问题