python requests post登陆信息 需要把cookies添加到header 该怎么处理

python requests post登陆信息 需要把cookies添加到header 该怎么处理。

尝试过
r=request.get(url)
然后 r.cookies 转成字符串添加到header,但是返回仍然不行。

阅读 10.1k
1 个回答
import requests
r = requests.get(url1)  # 你第一次的url
headers = {
    'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding':'gzip, deflate, sdch',
    'Accept-Language':'zh-CN,zh;q=0.8',
    'Connection':'keep-alive',
    'Cache-Control':'no-cache',
    'Content-Length':'6',
    'Content-Type':'application/x-www-form-urlencoded; charset=UTF-8',
    'Host':'www.mm131.com',
    'Pragma':'no-cache',
    'Origin':'http://www.mm131.com/xinggan/',
    'Upgrade-Insecure-Requests':'1',
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36',
    'X-Requested-With':'XMLHttpRequest'
}  # headers的例子,看你的post的headers
headers['cookie'] = ';'.join([headers['cookie'], ['='.join(i) for i in r.cookies.items()]])
r = requests.post(url2, headers=headers, data=data)  # 你第二次的url
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
宣传栏