我正在尝试使用python3
和requests
从FEMA下载zip文件。当我将URL粘贴到浏览器中以及wget
时,URL也会起作用。有一个重定向,我看到它已使用requests
成功重定向。但是,返回的内容比我期望的字节少得多,例如格式错误的zip。
以下是网址和代码段:
>>> import requests
>>> url = 'https://hazards.fema.gov/femaportal/NFHL/Download/ProductsDownLoadServlet?DFIRMID=10001C&state=DELAWARE&county=KENT COUNTY&fileName=10001C_20190130.zip'
>>> resp = requests.get(url)
>>> len(resp.content)
2583
>>> resp.headers['content-length']
'66892906'
>>> resp.url
'https://hazards.fema.gov/nfhlv2/output/County/10001C_20190130.zip'
更新:我正在docker容器中运行它。在容器外部,它按预期工作。
Python 3.7.3 (default, May 10 2019, 15:15:13)
[Clang 10.0.1 (clang-1001.0.46.3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import requests
>>> requests.__version__
'2.21.0'
>>> url = 'https://hazards.fema.gov/nfhlv2/output/County/10001C_20190130.zip'
>>> r = requests.get(url)
>>> len(r.content)
66892906
>>>