大写牛逼,用 Python 登录主流 24 个网站
Python技术
共 3529字,需浏览 8分钟
·
2020-11-17 01:10
▍已完成的主流网站
▍模拟登录GitHub
"""
github第二种登录方式
info:
author:CriseLYJ
github:https://github.com/CriseLYJ/
update_time:2019-3-7
"""
import re
import requests
from lxml import etree
class Login(object):
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.session = requests.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)
response = self.session.post(self.post_url, data=post_data, headers=self.headers)
print(response.status_code)
print(post_data)
if response.status_code == 200:
print("登录成功!")
else:
print("登录失败!")
# 获取token信息
# Get login token
def get_token(self):
response = self.session.get(self.login_url, headers=self.headers)
html = etree.HTML(response.content.decode())
token = html.xpath('//input[@name="authenticity_token"]/@value')[0]
return token
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('请输入您的账号: ')
password = input('请输入您的密码: ')
email = input('Account:')
password = input('Password:')
login = Login(email, password)
login = GithubLogin(email, password)
login.login_GitHub()
PS:公号内回复「Python」即可进入Python 新手学习交流群,一起 100 天计划!
老规矩,兄弟们还记得么,右下角的 “在看” 点一下,如果感觉文章内容不错的话,记得分享朋友圈让更多的人知道!
【神秘礼包获取方式】
评论