python 模拟登陆github的示例
作者:Kr1s77 时间:2022-01-05 17:52:41
# -*- coding: utf-8 -*-
# @Author: CriseLYJ
# @Date: 2020-08-14 12:13:11
import re
import requests
class GithubLogin(object):
def __init__(self, email, password):
# 初始化信息
self.headers = {
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_2) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/71.0.3578.98 Safari/537.36',
'Referer': 'https://github.com/',
'Host': 'github.com'
}
self.session = requests.Session()
self.login_url = 'https://github.com/login'
self.post_url = 'https://github.com/session'
self.email = email
self.password = password
def login_GitHub(self):
# 登录入口
post_data = {
'commit': 'Sign in',
'utf8': '✓',
'authenticity_token': self.get_token(),
'login': self.email,
'password': self.password
}
resp = self.session.post(
self.post_url, data=post_data, headers=self.headers)
print('StatusCode:', resp.status_code)
if resp.status_code != 200:
print('Login Fail')
match = re.search(r'"user-login" content="(.*?)"', resp.text)
user_name = match.group(1)
print('UserName:', user_name)
# Get login token
def get_token(self):
response = self.session.get(self.login_url, headers=self.headers)
if response.status_code != 200:
print('Get token fail')
return None
match = re.search(
r'name="authenticity_token" value="(.*?)"', response.text)
if not match:
print('Get Token Fail')
return None
return match.group(1)
if __name__ == '__main__':
email = input('Account:')
password = input('Password:')
login = GithubLogin(email, password)
login.login_GitHub()
登录效果
来源:https://github.com/Kr1s77/awesome-python-login-model
标签:python,登录,github
0
投稿
猜你喜欢
python机器学习库scikit-learn:SVR的基本应用
2022-04-25 09:38:34
pytorch使用horovod多gpu训练的实现
2022-01-07 16:01:18
如何让python程序正确高效地并发
2021-10-14 20:33:54
Python Socket编程入门教程
2022-03-08 01:08:49
Python定制类你不知道的魔术方法
2022-10-26 11:26:40
SQL Server TEXT、NTEXT字段拆分的问题
2008-10-26 12:28:00
python爬虫之验证码篇3-滑动验证码识别技术
2021-08-19 07:15:38
python读写文件操作示例程序
2021-04-03 13:09:49
自由落体的DIV
2010-01-22 15:40:00
python 装饰器的使用与要点
2023-02-05 01:04:02
python模块内置属性概念及实例
2023-11-23 15:34:27
Python OpenCV去除字母后面的杂线操作
2023-08-02 15:18:47
XML与HTML的结合(上)
2008-09-05 17:19:00
python爬虫 urllib模块url编码处理详解
2021-09-13 02:24:37
Python Pandas批量读取csv文件到dataframe的方法
2022-12-15 17:05:03
python-sys.stdout作为默认函数参数的实现
2022-11-23 02:50:35
Python中关于浮点数的冷知识
2023-11-13 14:01:57
如何解决MySQL的客户端不支持鉴定协议
2008-11-27 17:10:00
golang中的单引号转义问题
2023-07-07 07:20:22
实例讲解Python脚本成为Windows中运行的exe文件
2023-07-15 02:14:31