无法使用python请求下载pdf文件

2024-04-16 14:22:35 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用python请求下载一个文件。我可以下载图像,但pdf文件的内容是空的。在

class Scraper():
  def __init__(self, username=USERNAME, password=PASSWORD, 
               base_url=BASE_URL, login_url=LOGIN_URL, debug=False):
    self.session = session()
    self.authent()
    if debug:
      debug_http_request()

  def get(self, url, *arg, **kw):
    r = None
    for i in range(REPLAY_LIMIT):
      ld('getting %s (count %d)...' % (url, i))
      r = self.session.get(url, headers=FF_USER_AGENT, 
                           allow_redirects=False)
      ld('response code %d ' % r.status_code)
      if r.status_code in (200, 201):
        return r
      if r.status_code == 302 and r.url == BASE_URL:
        li("redirected to >> " + r.url)
        self.authent()
    return r

  def get_files_content(self, file_ids):
    for f in set(file_ids):
      url = ("very long url multiple lines string") % f
      file_result = self.get(url, stream=True)
      for block in file_result.iter_content(1024):
        if not block:
          break
        print block
      print "end of block"

当我试图通过以下方式获取文件内容时:

^{pr2}$

我得到以下调试结果:

reply: 'HTTP/1.1 200 OK\r\n'
header: Date: Fri, 27 Sep 2013 14:29:51 GMT
header: Server: Apache/2.2.16 (Debian)
header: X-Powered-By: PHP/5.3.18-1~dotdeb.0
header: Expires: Mon, 26 Jul 1997 05:00:00 GMT
header: Content-Transfer-Encoding: binary
header: Cache-control: private, must-revalidate
header: Pragma: no-cache
header: Content-Disposition: attachment; filename="the wanted file name";
header: Last-Modified: Fri, 27 Sep 2013 14:29:51 GMT
header: Content-Length: 0
header: Keep-Alive: timeout=15, max=96
header: Connection: Keep-Alive
header: Content-Type: application/pdf;

回答中没有任何内容。 仅供参考,下面的代码可以完美地与其他文档类似的图像一起使用。非常感谢。在


Tags: 文件indebugselfurl内容getif