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

在这段代码中

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

谢谢 !

解决方法:

正如documentation解释的那样,urllib2.urlopen返回一个带有info()方法的对象,该方法返回标头.

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

info = response.info()

for header in info.headers:

print header,

标签:response-headers,python

来源: https://codeday.me/bug/20191119/2038557.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值