python - 请求 : cannot download zip file properly with verify is False

标签 python ssl download python-requests

我正在使用 Python 2.7.3 和请求(请求==2.10.0)。我正在尝试从某个链接获取一些 zipfile。该网站的证书未经过验证,但我只想下载该 zip,因此我使用了 verfiy=False

link = 'https://webapi.yanoshin.jp/rde.php?https%3A%2F%2Fdisclosure.edinet-fsa.go.jp%2FE01EW%2Fdownload%3Fuji.verb%3DW0EZA104CXP001006BLogic%26uji.bean%3Dee.bean.parent.EECommonSearchBean%26lgKbn%3D2%26no%3DS1007NMV'
r = requests.get(link, timeout=10, verify=False)
print r.content
# 'GIF89a\x01\x00\x01\x00\x80\x00\x00\x00\x00\x00\xff\xff\xff!\xf9\x04\x01\x00\x00\x01\x00,\x00\x00\x00\x00\x01\x00\x01\x00@\x02\x02L\x01\x00;'
print r.headers
# {'Content-Length': '43', 'Via': '1.0 localhost (squid/3.1.19)', 'X-Cache': 'MISS from localhost', 'X-Cache-Lookup': 'MISS from localhost:3128', 'Server': 'Apache', 'Connection': 'keep-alive', 'Date': 'Mon, 06 Jun 2016 07:59:52 GMT', 'Content-Type': 'image/gif'}

但是,我尝试使用 Firefox 和 Chromium,如果我选择信任该证书,我将能够下载 zip 文件。 wget --no-check-certificate [that link] 也会生成大小正确的 zip 文件。

(我把那个gif写到磁盘上看了看,没有内容,文件太小了)

也许这是标题问题?我不知道。我当然可以使用wget。只是想弄清楚这背后的原因并使它起作用。

(浏览器会下载一些 zip,23.4KB)(wget [link] -O test.zip 也会下载 zip 文件)

最佳答案

服务器试图阻止脚本下载 ZIP 文件;使用 curl 时,您会看到同样的问题:

$ curl -sD - -o /dev/null "https://webapi.yanoshin.jp/rde.php?https%3A%2F%2Fdisclosure.edinet-fsa.go.jp%2FE01EW%2Fdownload%3Fuji.verb%3DW0EZA104CXP001006BLogic%26uji.bean%3Dee.bean.parent.EECommonSearchBean%26lgKbn%3D2%26no%3DS1007NUS"
HTTP/1.1 302 Found
Server: nginx
Date: Mon, 06 Jun 2016 08:56:20 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
X-Powered-By: PHP/7.0.7
Location: https://disclosure.edinet-fsa.go.jp/E01EW/download?uji.verb=W0EZA104CXP001006BLogic&uji.bean=ee.bean.parent.EECommonSearchBean&lgKbn=2&no=S1007NUS

注意 text/html 响应。

服务器似乎在寻找特定于浏览器的 AcceptUser-Agent header ;复制 Chrome 发送的 Accept header ,加上一个最小的 User-Agent 字符串,似乎足以欺骗服务器:

>>> r = requests.get(link, timeout=10, headers={'User-Agent': 'Mozilla/5.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8'}, verify=False)
# ... two warnings about ignoring the certificate ...
>>> r.headers
{'Content-Length': '14078', 'Content-Disposition': 'inline;filename="Xbrl_Search_20160606_175759.zip"', 'Set-Cookie': 'FJNADDSPID=3XWzlS; expires=Mon, 05-Sep-2016 08:57:59 GMT; path=/; secure, JSESSIONID=6HIMAP1I60PJ2P9HC5H3AC1N68PJAOR568RJIEB5CGS3I0UITOI5A08000P00000.E01EW_001; Path=/E01EW; secure', 'Connection': 'close', 'X-UA-Compatible': 'IE=EmulateIE9', 'Date': 'Mon, 06 Jun 2016 08:57:59 GMT', 'Content-Type': 'application/octet-stream'}

关于python - 请求 : cannot download zip file properly with verify is False,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37652964/

相关文章:

ssl - 在 SSL 连接中禁用弱密码

nginx - 需要来自 Nginx 的证书链(在传入接口(interface)上)

node.js - 使用 Node.js 和 Amazon S3 将文件直接传送到客户端

c# - 下载远程图像

python - 如何将临时 .docx 文件添加到 django 中的 zip 存档中

python - Django View 可以接收列表作为参数吗?

python - 启动 docker 服务时运行多个命令

ssl - 获取 SSL 连接以使用 STUNNEL/Win32

php - 如何在不执行的情况下下载php文件?

python - 如何使用Python提高PostGIS中插入数据的效率?