Python 请求不会返回我可以通过curl 获得的内容长度 header 。
def get_headers(url):
response = requests.head(url)
return response.headers
回应:
{'Server': 'nginx/1.17.9', 'Date': 'Sun, 24 May 2020 19:09:02 GMT', 'Content-Type': 'text/html; charset=utf-8', 'Connection': 'keep-alive', 'Vary': 'Accept-Encoding', 'Access-Control-Allow-Origin': '*', 'Content-Disposition': 'inline; filename="introduction - Sia API Documentation.html"', 'Skynet-File-Metadata': '{"filename":"introduction - Sia API Documentation.html"}', 'Access-Control-Expose-Headers': 'skynet-file-metadata', 'Content-Encoding': 'gzip'}
我也尝试过 requests.get,但也不起作用:
response = requests.get(url,stream=True)
最佳答案
如果您查看返回的 header ,您会注意到:
传输编码:分块
但是,如果您使用 httpbin 比较发送的 header ,您可能会注意到 Requests 还发送 Accept-Encoding: gzip, deflate。如果您删除该 header ,例如,
r = requests.head(url, headers={'Accept-Encoding': None})
然后您将获得 Content-Length header 。
关于Python请求: missing content-length response,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61991164/