【发布时间】:2015-07-07 21:29:52
【问题描述】:
我正在尝试下载此zip file。 zip 文件通过 chrome 正确下载,但使用 requests 或 urllib 失败并出现 400 Bad Request 错误。
>> import requests
>> import urllib
>> url = 'http://prd-enforce-xfr-02.dol.gov/../data_catalog/EBSA/ebsa_ocats_20150703.csv.zip'
>> r = requests.get(url)
>> r.ok
False
>> r.headers
{'content-length': '254', 'content-encoding': 'gzip', 'vary': 'Accept-Encoding', 'server': 'Apache/2.2.14 (Ubuntu)', 'connection': 'close', 'date': 'Tue, 07 Jul 2015 20:39:55 GMT', 'content-type': 'text/html; charset=iso-8859-1'}
>> r
<Response [400]>
>> r.text
u'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>400 Bad Request</title>\n</head><body>\n<h1>Bad Request</h1>\n<p>Your browser sent a request that this server could not understand.<br />\n</p>\n<hr>\n<address>Apache/2.2.14 (Ubuntu) Server at prd-enforce-xfr-02.dol.gov Port 80</address>\n</body></html>\n'
>> z = urllib.urlopen(url)
>> z.read()
'<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">\n<html><head>\n<title>400 Bad Request</title>\n</head><body>\n<h1>Bad Request</h1>\n<p>Your browser sent a request that this server could not understand.<br />\n</p>\n<hr>\n<address>Apache/2.2.14 (Ubuntu) Server at prd-enforce-xfr-02.dol.gov Port 80</address>\n</body></html>\n'
我尝试过(但失败了)的事情:
欺骗 User-Agent 标头。
使用请求会话持久化 cookie
.
【问题讨论】:
标签: python python-requests urllib