我尝试使用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"
当我试图通过以下方式获取文件内容时:
ser = Scraper(debug=True)
print ser.get_files_content([60857])
我得到以下调试结果:
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;
响应中没有内容。 仅供参考,以下代码与其他文档(如图像)完美配合。非常感谢。
答案 0 :(得分:3)
您的服务器还认为PDF文件的长度为零:
Content-Length: 0
请调试服务器上的问题。也许上传出错了?