Python urllib2响应头

loo*_*ter 24 python url header response

我正在尝试提取URL请求的响应头.当我使用firebug分析URL请求的响应输出时,它返回:

Content-Type text/html
Run Code Online (Sandbox Code Playgroud)

但是当我使用python代码时:

urllib2.urlopen(URL).info()
Run Code Online (Sandbox Code Playgroud)

结果输出返回:

Content-Type: video/x-flv
Run Code Online (Sandbox Code Playgroud)

我是python的新手,也是Web编程的新手; 非常感谢任何有用的见解.另外,如果需要更多信息,请告诉我.

提前感谢阅读这篇文章

qin*_*gbo 37

尝试像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')
Run Code Online (Sandbox Code Playgroud)

还有HTTPCookieProcessor可以使它更好,但我认为在大多数情况下你不需要它.看看python的文档:

http://docs.python.org/library/urllib2.html

  • 对于Python 3:`response.info()["content-type"]` (4认同)

bob*_*nce 5

内容类型文本/ html

真的,没有结肠吗?

如果是这样,那可能会解释它:它是一个无效的头,所以它被忽略,所以urllib通过查看文件名来猜测内容类型.如果URL恰好在末尾有".flv",那么它应该猜测类型video/x-flv.


Ale*_*lli 2

这种特殊的差异可能是由两个请求发送的不同标头(可能是接受类型的标头)来解释——你能检查一下吗……?或者,如果 Javascript 在 Firefox 中运行(我假设您在运行 firebug 时使用的是 Firefox?)——因为它绝对不能在 Python 中运行——正如他们所说,“一切皆有可能”;-) 。