简体   繁体   English

如何使用Python urlopen()获取HTTP响应头

[英]How to get HTTP response headers with Python urlopen()

In this code 在这段代码中

from bs4 import BeautifulSoup
import urllib2
import re

html_page = urllib2.urlopen("http://fr.wikipedia.org/wiki/Alan_Turing")

soup = BeautifulSoup(html_page, "lxml")

print soup

I can return source code. 我可以返回源代码。

But how having http headers (in Python), please ? 但是如何拥有http标头(在Python中)?

Example : 示例:

HTTP/1.1 200 OK
Server: nginx/1.9.4
Date: Thu, 10 Sep 2015 09:13:25 GMT
Content-Type: text/css; charset=utf-8
Content-Length: 10699
x-content-type-options: nosniff
Cache-Control: public, max-age=300, s-maxage=300
X-Powered-By: HHVM/3.6.5
Access-Control-Allow-Origin: *
Vary: Accept-Encoding
Expires: Thu, 10 Sep 2015 09:16:07 GMT
Content-Encoding: gzip
Accept-Ranges: bytes
Age: 138

Thanks ! 谢谢 !

As the documentation explains, urllib2.urlopen returns an object with an info() method which returns the headers. 正如文档所解释的那样, urllib2.urlopen返回一个带有info()方法的对象,该方法返回标题。

response = urllib2.urlopen("http://fr.wikipedia.org/wiki/Alan_Turing")
info = response.info()
for header in info.headers:
    print header,

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM