【问题标题】:Using PDFMiner (Python) with online pdf files. Encode the url?使用 PDFMiner (Python) 处理在线 pdf 文件。编码网址?
【发布时间】:2014-04-21 04:08:12
【问题描述】:

我希望使用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)

稍作改动后效果很好。

现在,我尝试使用urllib2.openurl 获取在线 PDF,但这不起作用。我收到一条错误消息:coercing to Unicode: need string or buffer, instance found

如何从urllib2.openurl 获取字符串(或其他任何内容),以便在我给它一个 PDF 文件名(而不是 URL)时它与 open 函数相同?

如果我的问题不清楚,请告诉我。

【问题讨论】:

    标签: python pdf urllib2 fetch urllib


    【解决方案1】:

    嗯,我终于找到了解决办法,

    我求助于RequestStringIO 并摆脱了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 视为一个文件(这么说)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-12-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-18
      相关资源
      最近更新 更多