[Python]requests 得到headers解析为dict

本文介绍如何使用Python的requests库获取HTTP响应头,并将其转换为字典格式进行解析。通过实例展示了headers的常见字段及其含义,包括Content-Type、Server等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

requests 得到headers解析为dict

r = requests.get("http:...")
In [99]: type(r.headers)
Out[99]: requests.structures.CaseInsensitiveDict
In [96]: json.dumps(dict(r.headers))
Out[96]: '{"x-powered-by": "PHP/5.3.3", "transfer-encoding": "chunked", "content-encoding": "gzip", "server": "nginx/1.8.0", "connection": "keep-alive", "date": "Mon, 25 Jan 2016 09:56:54 GMT", "content-type": "text/html; charset=UTF-8", "x-pingback": "http://xiaorui.cc/xmlrpc.php"}'
 
In [97]: json.loads(json.dumps(dict(r.headers)))
Out[97]:
{u'connection': u'keep-alive',
 u'content-encoding': u'gzip',
 u'content-type': u'text/html; charset=UTF-8',
 u'date': u'Mon, 25 Jan 2016 09:56:54 GMT',
 u'server': u'nginx/1.8.0',
 u'transfer-encoding': u'chunked',
 u'x-pingback': u'http://xiaorui.cc/xmlrpc.php',
 u'x-powered-by': u'PHP/5.3.3'}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值