我正在尝试提取 URL 请求的响应标头。当我使用firebug分析URL请求的响应输出时,它返回:
Content-Type text/html
但是当我使用 python 代码时:
urllib2.urlopen(URL).info()
结果输出返回:
Content-Type: video/x-flv
我对 python 和一般的网络编程都很陌生;非常感谢任何有用的见解。另外,如果需要更多信息,请告诉我。
预先感谢您阅读这篇文章
尝试像 Firefox 那样请求。您可以在 Firebug 中看到请求标头,因此将它们添加到您的请求对象中:
import urllib2
request = urllib2.Request('http://your.tld/...')
request.add_header('User-Agent', 'some fake agent string')
request.add_header('Referer', 'fake referrer')
...
response = urllib2.urlopen(request)
# check content type:
print response.info().getheader('Content-Type')
还有 HTTPCookieProcessor 可以让它变得更好,但我认为在大多数情况下你不需要它。看看python的文档:
http://docs.python.org/library/urllib2.html http://docs.python.org/library/urllib2.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)