AI写作智能体 自主规划任务,支持联网查询和网页读取,多模态高效创作各类分析报告、商业计划、营销方案、教学内容等。 广告
不使用`session.post`请求,使⽤cookie也可以获取登录后的页面。 * cookie是有过期时间的,一旦过期需要重新获取; <br/> 我们可以在headers中使用cookie,也可以使用`request.get(url, cookies)`参数,下面使用两种方法登录我的码云账号。 ![](https://img.kancloud.cn/ca/a0/caa0c88ba77f1a7842ab817066dee6d6_1230x664.png) <br/> **1. 在headers使用cookie登录码云** ```python """ @Date 2021/4/24 """ import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36', 'Cookie': 'oschina_new....' \ 'tbW93ZElueVBQYmhUUjBMbm9KMHpWUT09--14ba35d74cc21de112593cc296711434786e6fce' } r = requests.get("https://gitee.com/xxx/dashboard/projects", headers=headers) print(r.text) ``` <br/> **2. 使用cookies参数登录码云** ```python """ @Date 2021/4/24 """ import requests headers = { 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36', } cookie = 'oschina_new_user=false; remove_bulletin=gitee-maintain-1592359975; ' \ '...;remote_way=http; -14ba35d74cc21de112593cc296711434786e6fce' # 1. 将cookie字符串转换为字典 cookies = {i.split('=')[0]:i.split('=')[1] for i in cookie.split('; ')} # 2. 使用cookies参数 r = requests.get("https://gitee.com/xxx/dashboard/projects", headers=headers, cookies=cookies) print(r.text) ```