如何调用 urllib2 get_header 方法? [英] How to call urllib2 get_header method?
问题描述
我正在调查 python urllib2 下载大小 问题.
尽管 RanRag 或 jterrace
建议的方法对我来说效果很好,但我想知道如何使用 urllib2.Request.get_header 方法实现相同.所以,我尝试了以下代码行:
因为,您可以看到 get_header
没有返回任何内容,header_items
也没有返回.
那么,使用上述方法的正确方法是什么?
urllib2.Request
类只是URL 请求的抽象"(http://docs.python.org/library/urllib2.html#urllib2.Request),并且不做任何实际的数据检索.您必须使用 urllib2.urlopen
来检索数据.urlopen
要么将 url 直接作为字符串,也可以传递一个 Request
对象的实例.
例如:
<预><代码>>>>req_info = urllib2.urlopen('https://www.google.com/logos/2012/javelin-2012-hp.jpg')>>>req_info.headers.keys()['content-length', 'x-xss-protection', 'x-content-type-options', 'expires', 'server', 'last-modified', 'connection', 'cache-control', '日期', '内容类型']>>>req_info.headers.getheader('内容长度')'52741'I was looking into python urllib2 download size question.
Although the method RanRag or jterrace
suggested worked fine for me but I was wondering how to use the urllib2.Request.get_header method to achieve the same. So, I tried the below line of code:
>>> import urllib2
>>> req_info = urllib2.Request('http://mirror01.th.ifl.net/releases//precise/ubuntu-12.04-desktop-i386.iso')
>>> req_info.header_items()
[]
>>> req_info.get_header('Content-Length')
>>>
As, you can see the get_header
returned nothing and neither does header_items
.
So, what is the correct way to use the above methods?
The urllib2.Request
class is just "an abstraction of a URL request" (http://docs.python.org/library/urllib2.html#urllib2.Request), and does not do any actual retrieval of data. You must use urllib2.urlopen
to retrieve data. urlopen
either takes the url directly as a string, or you can pass an instance of the Request
object too.
For example:
>>> req_info = urllib2.urlopen('https://www.google.com/logos/2012/javelin-2012-hp.jpg')
>>> req_info.headers.keys()
['content-length', 'x-xss-protection', 'x-content-type-options', 'expires', 'server', 'last-modified', 'connection', 'cache-control', 'date', 'content-type']
>>> req_info.headers.getheader('Content-Length')
'52741'
这篇关于如何调用 urllib2 get_header 方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!