损坏的 zip 下载 urllib2 [英] corrupt zip download urllib2

查看:27
本文介绍了损坏的 zip 下载 urllib2的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码从 measurehs.com 下载 zip 文件:

I am trying to download zip files from measuredhs.com using the following code:

url ='https://dhsprogram.com/customcf/legacy/data/download_dataset.cfm?Filename=BFBR62DT.ZIP&Tp=1&Ctry_Code=BF'

request = urllib2.urlopen(url)

output = open("install.zip", "w") 
output.write(request.read()) 
output.close()

但是下载的文件打不开.我收到一条消息,说压缩的 zip 文件夹无效.

However the downloaded file does not open. I get a message saying the compressed zip folder is invalid.

要访问下载链接,需要长时间输入,我已经这样做了.如果我点击链接,它会自动下载文件,或者即使我将其粘贴到浏览器中.

To access the download link, one needs to long in, which I have done so. If i click on the link, it automatically downloads the file, or even if i paste it in a browser.

谢谢

推荐答案

尝试以二进制模式写入本地文件.

Try writing to local file in binary mode.

with open('install.zip', 'wb') as output:
    output.write(request.read())

此外,比较下载文件的 md5/sha1 哈希值可以让您知道下载的文件是否已损坏.

Also, comparing the md5/sha1 hash of the downloaded file will let you know if the downloaded file has been corrupted.

这篇关于损坏的 zip 下载 urllib2的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆