首页 > requests库无法设置headers

requests库无法设置headers

pythonimport requests
url = 'http://www.baidu.com'
headers = {'Content-Type': 'application/json'}

r = requests.get(url, headers=headers)

print r.headers['Content-Type']

输出的内容为text/html; charset=utf-8 而不是我设置的application/json

何解?

BTW python2.7.9


这个只是一种协议, 如果服务器不支持, 或者说是忽略了这个那没办法
不是 requests的问题....


因为这个 www.baidu.com 返回的内容就是 text/html; charset=utf-8;
设置的headers = {'Content-Type': 'application/json'}是表示你传入的数据是json格式。
这是你设置的定制请求头,还不是响应头。


Content-Type 是 Response Header属性,而不是Request Header属性。Request header的属性有User-Agent、Accept、Accept-Language、Cookie以及Connection等等。

【热门文章】
【热门文章】