利用Python模拟GitHub登录

转载链接:https://www.cnblogs.com/wintest/p/11183744.html

代码:

import requests
import re
#去掉警告 (#ssl-warnings InsecureRequestWarning)
import urllib3
urllib3.disable_warnings()

class Github_Login():
def __init__(self):
self.s = requests.session() #设置session
# 设置请求头
self.headers = {
"User-Agent":"Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 \
(KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36"
}
#在使用Fiddler时进行请求,通过该代码忽略SSLError错误
self.s.verify = False
#获取 authenticity_token
def get_authenticity_token(self):
login_url = "https://github.com/login"
r = self.s.get(login_url, headers = self.headers)
authenticity_token = re.findall('<input type="hidden" name="authenticity_token" value="(.+?)" />', r.text)
print("authenticity_token:{}".format(authenticity_token))
return authenticity_token[0]
#模拟登录,并返回title
def github_login(self, authenticity_token, username, password):
session_url = "https://github.com/session"
body = {
"authenticity_token":authenticity_token,
"commit":"Sign in",
"login":username,
"password":password,
"utf9":"✓",
"webauthn-support": "unknown"
}
r = self.s.post(session_url, headers =self.headers, data = body)
title = re.findall('<title>(.+?)</title>', r.text)
print("title:%s" %title[0])
return title[0]
#通过title判断是否登录成功
def is_login_success(self, title):
if "GitHub" == title:
return True
else:
return False
if __name__ == '__main__':
github = Github_Login()
authenticity_token = github.get_authenticity_token()
title = github.github_login(authenticity_token, username = "g123",password = "ceshi1")
login_result = github.is_login_success(title)
print(login_result)

posted on 2020-11-27 14:08  FanF  阅读(240)  评论(0)    收藏  举报

导航