使用 python-requests 获取文件大小,同时只获取标题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14270698/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get file size using python-requests, while only getting the header
提问by scandinavian_
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?
我查看了请求文档,但似乎找不到任何内容。我如何只请求标题,以便我可以评估文件大小?
采纳答案by Blender
Send a HEAD request:
发送HEAD 请求:
>>> 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.
HEAD 请求类似于只下载标头的 GET 请求。请注意,实际上由服务器来满足您的 HEAD 请求。某些服务器只会响应 GET 请求,因此您必须发送 GET 请求并关闭连接而不是下载正文。其他时候,服务器从不指定文件的总大小。
回答by watashiSHUN
use requests.get(url, stream=True).headers['Content-length']
用 requests.get(url, stream=True).headers['Content-length']
stream=Truemeans when function returns, only the response header is downloaded, response body is not.
stream=True意味着当函数返回时,只下载响应头,不下载响应体。
Both requests.getand request.headcan get you headers but there's an advantage of using get
双方requests.get并request.head能得到你的信息,但是有使用的优势get
getis more flexible, if you want to download the response body after inspecting the length, you can start by simply access thecontentproperty or using aniteratorwhich will download the content in chunks- "HEAD request SHOULD be identical to the information sent in response to a GET request."but its not always the case.
get更灵活,如果您想在检查长度后下载响应正文,您可以通过简单地访问content属性或使用iterator将分块下载内容的an开始- “HEAD 请求应该与响应 GET 请求而发送的信息相同。” 但情况并非总是如此。
here is an example of getting the length of a MIT open course video
这是获取MIT 公开课程视频长度的示例
MitOpenCourseUrl = "http://www.archive.org/download/MIT6.006F11/MIT6_006F11_lec01_300k.mp4"
resHead = requests.head(MitOpenCourseUrl)
resGet = requests.get(MitOpenCourseUrl,stream=True)
resHead.headers['Content-length'] # output 169
resGet.headers['Content-length'] # output 121291539

