py 登陸github時token以及cookie的應用

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
)

更多精彩文章請關注 王明昌博客html

相關文章
相關標籤/搜索