使用python urllib2获取位置标头的值 [英] getting value of location header using python urllib2
本文介绍了使用python urllib2获取位置标头的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
当我使用urllib2并列出标题时,我看不到位置标题。
when I use urllib2,and list the headers,I cannot see the 'Location' header.
In [19]:p = urllib2.urlopen('http://www.example.com')
In [21]: p.headers.items()
Out[21]:
[('transfer-encoding', 'chunked'),
('vary', 'Accept-Encoding'),
('server', 'Apache/2.2.3 (CentOS)'),
('last-modified', 'Wed, 09 Feb 2011 17:13:15 GMT'),
('connection', 'close'),
('date', 'Fri, 25 May 2012 03:00:02 GMT'),
('content-type', 'text/html; charset=UTF-8')]
如果我使用telnet和GET
If I use telnet and GET
telnet www.example.com 80
Trying 192.0.43.10...
Connected to www.example.com.
Escape character is '^]'.
GET / HTTP/1.0
Host:www.example.com
HTTP/1.0 302 Found
Location: http://www.iana.org/domains/example/
Server: BigIP
Connection: close
Content-Length: 0
那么,使用urllib2,如何获得'Location'标题的值?
So, using urllib2 , how do I get the value of 'Location' header?
推荐答案
使用 geturl
从 urlopen
返回类文件对象的方法:
Use the geturl
method on the returned file-like object from urlopen
:
>>> f = urllib2.urlopen('http://www.example.com')
>>> f.geturl()
'http://www.iana.org/domains/example/'
这篇关于使用python urllib2获取位置标头的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文