爬虫篇

import requests
import re
# 第一次请求
r1 = requests.get("https://github.com/login")
r1_cookie = r1.cookies.get_dict()  # 拿到初始cookie(未被授权)
authenticity_token = re.findall(r'name="authenticity_token".*?value="(.*?)"',r1.text)[0]  # 从页面上中拿到CSRF TOKEN

# 第二次请求: 带着初始的cookie 和TOKEN发送POST 请求给登录页面,带上账号和密码
data = {
    'commit':'Sign in',
    'utf8':'✓',
    'authenticity_token':authenticity_token,
    'login':'317828332@qq.com',
    'password':'alex3714'
}
r2 = requests.post("https://github.com/session",
                   data=data,
                   cookies=r1_cookie)
login_cookie = r2.cookies.get_dict()
# 第三次请求 :以后的登录,拿着login_cookie 就可以了,比如访问一些个人配置
r3 = requests.get("https://github.com/settings/emails",cookies=login_cookie)
print("317828332@qq.com" in r3.text)
原文地址:https://www.cnblogs.com/zhanglin123/p/8761412.html