corrupt zip download urllib2

只愿长相守 提交于 2019-12-13 18:49:41

问题


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()

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.

Thanks


回答1:


Try writing to local file in binary mode.

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

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



来源:https://stackoverflow.com/questions/25016115/corrupt-zip-download-urllib2

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!