python+requests——检查响应头是否存在
Posted 小白龙白龙马
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python+requests——检查响应头是否存在相关的知识,希望对你有一定的参考价值。
import requests resp = requests.get(‘http://httpbin.org/get‘) print(type(resp.headers)) print(resp.headers[‘Content-Type‘]) actual_headers = {} for k,v in resp.headers.items(): #把响应头转为小写 actual_headers[k.lower()] = v print(actual_headers) for k,v in actual_headers.items(): if k ==‘content-type‘: print(actual_headers[k]) for k,v in actual_headers.items(): if k ==‘content-type‘ and actual_headers[k] == ‘application/json‘: print(‘存在‘)
执行结果:
<class ‘requests.structures.CaseInsensitiveDict‘>
application/json
{‘date‘: ‘Sun, 23 Feb 2020 11:21:17 GMT‘, ‘content-type‘: ‘application/json‘, ‘content-length‘: ‘307‘, ‘connection‘: ‘keep-alive‘, ‘server‘: ‘gunicorn/19.9.0‘, ‘access-control-allow-origin‘: ‘*‘, ‘access-control-allow-credentials‘: ‘true‘}
application/json
存在
以上是关于python+requests——检查响应头是否存在的主要内容,如果未能解决你的问题,请参考以下文章