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
存在

猜你喜欢

转载自www.cnblogs.com/xiaobaibailongma/p/12353355.html