使用 python-requests 获取文件大小,同时只获取标题 [英] Get file size using python-requests, while only getting the header
问题描述
我查看了请求文档,但似乎找不到任何内容.我如何只请求标题,以便我可以评估文件大小?
发送 HEAD 请求一个>:
<预><代码>>>>进口请求>>>response = requests.head('http://example.com')>>>响应头文件{'连接':'关闭','内容编码':'gzip','内容长度':'606','内容类型':'文本/html;字符集=UTF-8','date': '2013 年 1 月 11 日星期五 02:32:34 GMT','last-modified': '星期五, 2013 年 1 月 4 日 01:17:22 GMT','服务器': 'Apache/2.2.3 (CentOS)','变化':'接受编码'}HEAD 请求就像一个 GET 请求,它只下载标头.请注意,实际上由服务器来满足您的 HEAD 请求.某些服务器只会响应 GET 请求,因此您必须发送 GET 请求并关闭连接而不是下载正文.其他时候,服务器从不指定文件的总大小.
I have looked at the requests documentation, but I can't seem to find anything. How do I only request the header, so I can assess filesize?
Send a HEAD request:
>>> import requests
>>> response = requests.head('http://example.com')
>>> response.headers
{'connection': 'close',
'content-encoding': 'gzip',
'content-length': '606',
'content-type': 'text/html; charset=UTF-8',
'date': 'Fri, 11 Jan 2013 02:32:34 GMT',
'last-modified': 'Fri, 04 Jan 2013 01:17:22 GMT',
'server': 'Apache/2.2.3 (CentOS)',
'vary': 'Accept-Encoding'}
A HEAD request is like a GET request that only downloads the headers. Note that it's up to the server to actually honor your HEAD request. Some servers will only respond to GET requests, so you'll have to send a GET request and just close the connection instead of downloading the body. Other times, the server just never specifies the total size of the file.
这篇关于使用 python-requests 获取文件大小,同时只获取标题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!