requests库无法设置headers



 python


 import 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

python requests

kichan 9 years, 4 months ago

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

嘻嘻大怪獸 answered 9 years, 4 months ago

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

Nocturn answered 9 years, 4 months ago

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

男汉子♂橙诚 answered 9 years, 4 months ago

Your Answer