py 登录github时token以及cookie的应用
程序员文章站
2022-05-02 11:41:43
...
import requests
from bs4 import BeautifulSoup
## 获取token
r1 = requests.get('https://github.com/login')
s1 = BeautifulSoup(r1.text,'html.parser')
token = s1.find(name='input',attrs = {
"name":'authenticity_token'
}).get('value')
r1_cookie_dict = r1.cookies.get_dict() #拿到字典类型的cookie#
## 发送数据
r2 = requests.post(
'https://github.com/session',
data={
"utf8":" ✓",
"authenticity_token": token,
"login": '用户名',
"password":'密码'
},
cookies = r1_cookie_dict
)
# 保存cookie
r2_cookie_dict = r2.cookies.get_dict()
##获取所有cookie值并更新
cookie_dict = {}
cookie_dict.update(r1_cookie_dict)
cookie_dict.update(r2_cookie_dict)
# 带cookie请求页面
r3 = requests.get(
url='******',
cookiss = cooke_dict
)
更多精彩文章请关注 王明昌博客