如何给session添加cookie、headers

就是简单的赋值就OK~

import requests

headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.87 Safari/537.36"}

# 请求知乎网
r = requests.get('https://www.zhihu.com/signin?next=%2F', headers=headers)

# 获取cookies
cookies = r.cookies
print(headers)
print(cookies)
print('-' * 100)

session = requests.Session()

# 将获取的cookies添加到session
session.cookies = cookies
session.headers = headers

# 带cookie请求登录界面
response = session.get('https://www.zhihu.com/signin?next=%2F')
print(response)
print(session.cookies)
print(session.headers)

运行结果如下图:
如何给session添加cookie、headers_第1张图片

你可能感兴趣的:(Python爬虫,session,python,cookie)