簡體   English   中英

如何使用Python urlopen()獲取HTTP響應頭

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

在這段代碼中

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

我可以返回源代碼。

但是如何擁有http標頭(在Python中)?

示例:

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

謝謝 !

正如文檔所解釋的那樣, 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