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

时间:2020-07-11 16:17:45

标签: python web-scraping download python-requests zip

当我访问此website时,浏览器会打开一个框,用于下载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(

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

1 个答案:

答案 0 :(得分:0)

我通过在网址末尾添加/使它正常工作

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)

查看实际情况here