dam*_*mon 2 python urllib2 http-headers
当我使用urllib2并列出标题时,我看不到'Location'标题.
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')]
Run Code Online (Sandbox Code Playgroud)
如果我使用telnet和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
Run Code Online (Sandbox Code Playgroud)
那么,使用urllib2,我如何获得'Location'标题的值?
这是因为默认情况下urllib2遵循位置标头.所以最后的回应不会有.如果您突然禁用了以下重定向,则可以看到301和302页面的位置标题.请参阅:如何阻止Python的urllib(2)遵循重定向
借用那里:
class NoRedirection(urllib2.HTTPErrorProcessor):
def http_response(self, request, response):
return response
https_response = http_response
opener = urllib2.build_opener(NoRedirection)
location = opener.open('http://www.example.com').info().getheader('Location')
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4039 次 |
| 最近记录: |