Shadow
使用python模拟登陆github网站
python登陆github网站
使用python模拟登陆Github网站,下面是代码要是没有 BeautifulSoup 解析库,请先安装。
安装命令:pip install beautifulsoup4
#!/usr/bin/python3
# -*- conding:utf-8 -*-
# 使用【Beautiful Soup】模拟登陆github网站
import requests
from bs4 import BeautifulSoup
if __name__ == '__main__':
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.198 Safari/537.36',
}
# 构建一个请求会话
with requests.session() as session:
session.headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.128 Safari/537.36'
}
# 先请求登陆页面
url = 'https://github.com/login'
res = session.get(url)
bs = BeautifulSoup(res.text,'lxml')
#登陆的post数据
loginInfo = {
'commit': 'Sign in',
'webauthn-support': 'supported',
'login':'username', #github账号
'password':'xxxx' #github密码
}
# 获取登陆的参数 authenticity_token
authInput = bs.find('input',attrs={'name':'authenticity_token'})
loginInfo['authenticity_token'] = authInput.attrs['value']
# 获取登陆的参数 timestamp_secret
authInput = bs.find('input',attrs={'name':'timestamp_secret'})
loginInfo['timestamp_secret'] = authInput.attrs['value']
# 获取登陆的参数 timestamp
authInput = bs.find('input',attrs={'name':'timestamp'})
loginInfo['timestamp'] = authInput.attrs['value']
# 提交登陆的地址
loginUrl = 'https://github.com/session'
# post提交登陆
res2 = session.post(loginUrl,data=loginInfo)
# 会员中心地址
profileUrl = "https://github.com/settings/profile"
# 获取会员中心地址
res3 = session.get(profileUrl)
res3H = BeautifulSoup(res3.text,'lxml')
print(res3H.title.text) #这行能打印出来 Your Profile 就算正常登陆成功了
Dcr163的博客
http://dcr163.cn/560.html(转载时请注明本文出处及文章链接)