使用python中的请求模块从URL下载zip文件

若昂·佩尼多(Joao Penido)

当我访问该网站时,我的浏览器会打开一个框以下载zip文件。

我正在尝试通过python脚本下载zip文件(我是编码的初学者)。我想在将来自动下载一批类似的链接,但是现在我只测试一个链接。这是我的代码:

import requests

url = 'https://sigef.incra.gov.br/geo/exportar/vertice/shp/454698fd-6dfa-49a1-8096-bd9bb57b62ca'
r = requests.get(url, verify=False, allow_redirects=True)

open('verticeshp454698fd-6dfa-49a1-8096-bd9bb57b62ca.zip', 'wb').write(r.content)

作为输出,我得到了一个损坏的zip文件,而不是我想要的文件。我还在命令提示符下收到以下消息:

C:\Users\joaop\AppData\Local\Programs\Python\Python38\lib\site-packages\urllib3\connectionpool.py:979: InsecureRequestWarning: Unverified HTTPS request is being made to host 'sigef.incra.gov.br'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#ssl-warnings
  warnings.warn(

我在这里错过了哪些步骤?在此先感谢您的帮助。

CodeIt

我通过/在URL末尾添加来使其工作

import requests

# the `/` at the end is important
url = 'https://sigef.incra.gov.br/geo/exportar/vertice/shp/454698fd-6dfa-49a1-8096-bd9bb57b62ca/'

headers = {
    "User-Agent": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2866.71 Safari/537.36", 
  }

r = requests.get(url, headers=headers, verify=False, allow_redirects=True)

# get the filename from the headers `454698fd-6dfa-49a1-8096-bd9bb57b62ca_vertice.zip`
filename = r.headers['Content-Disposition'].split("filename=")[-1]

with open(filename, 'wb') as f:
  f.write(r.content)

看到它在这里行动

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章