gpt4 book ai didi

python-3.x - 从 http 响应 header 下载内容配置 (Python 3)

转载 作者:行者123 更新时间:2023-12-03 16:29:23 26 4
gpt4 key购买 nike

我在这里寻求一点帮助。我一直在使用 Python 中的请求来访问网站。我能够访问该网站并获得响应 header ,但我不确定如何下载 Content-disposition 中包含的 zip 文件。我猜这不是 Requests 可以处理的函数,或者至少我似乎找不到任何关于它的信息。如何访问文件并保存它?

'Content-disposition': 'attachment;filename=MT0376_DealerPrice.zip'

最佳答案

使用 urllib 而不是 requests 因为它是一个更轻量级的库:

import urllib

req = urllib.request.Request(url, method='HEAD')
r = urllib.request.urlopen(req)
print(r.info().get_filename())

示例:

In[1]: urllib.request.urlopen(urllib.request.Request('https://httpbin.org/response-headers?content-disposition=%20attachment%3Bfilename%3D%22example.csv%22', method='HEAD')).info().get_filename()
Out[1]: 'example.csv'

关于python-3.x - 从 http 响应 header 下载内容配置 (Python 3),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28311735/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com