爬虫自动登陆GitHub
原创
©著作权归作者所有:来自51CTO博客作者aaronthon的原创作品,请联系作者获取转载授权,否则将追究法律责任
import requests
from bs4 import BeautifulSoup
r1 = requests.get(
url='https://github.com/login'
)
s1 = BeautifulSoup(r1.text, 'html.parser')
token = s1.find(name='input', attrs={'name': 'authenticity_token'}).get('value')
# 取到token值
r1_cookie_dict = r1.cookies.get_dict()
r2 = requests.post(
url='https://github.com/session',
data={
'commit':'Sign in',
'utf8':'✓',
'authenticity_token':token,
'login':'1439286684@qq.com',
'password':'123456' # 要输入正确的账号密码
},
cookies=r1_cookie_dict
)
r2_cookie_dict = r2.cookies.get_dict()
r3 = requests.get(
url='https://github.com/settings/emails',
cookies=r2_cookie_dict
)
# 如果打印的内容带有登陆用户的信息就是登陆成功了
print(r3.text)