我希望使用 PDFMiner
提取在线可用的 pdf 文件的内容。
我的代码基于 documentation 中可用的代码用于提取硬盘上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)
做一些小改动后效果很好。
现在,我已经为在线 PDF 尝试了 urllib2.openurl
,但它不起作用。我收到一条错误消息:强制转换为 Unicode:需要字符串或缓冲区,已找到实例
。
我怎样才能从 urllib2.openurl
得到一个字符串(或其他什么),这样当我给它一个 PDF 文件名时,它与 open
函数的内容相同(相对于 URL)`?
如果我的问题不清楚,请告诉我。
最佳答案
好吧,我终于找到了解决办法,
我求助于 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 视为一个文件(这么说)。
关于python - 使用 PDFMiner (Python) 处理在线 pdf 文件。对网址进行编码?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22429193/