模拟Github登录分为以下五个操作步骤。
Github登录过程模拟:
1、请求标头: self.headers,请求url;
2、设定session,保存登录信息cookies,生成github_cookie文件;
3、提交POST表格,要求数据格式post_data;
4、获取authenticity_token;
5、通过个人中心验证判断登录是否成功,输出个人中心的信息后登录成功。 另一方面,获取请求头在浏览器中输入https://github.com/login,同时右击页面查看检查。 下图:
点击红框内的login,则如下图所示。
源代码的对应部分:
# #请求头的设定
self.headers={
“参考者”:“https://github.com /”,
'用户代理' : ' Mozilla/5.0 (windows nt 10.0; WOW64 )苹果维基/537.36 (khtml,like Gecko ) chrome/57.0.2987.133 safari/537.36 ',
'主机: ' github.com '
(二、保存登录信息设置cookies设置session 保存登录信息cookies,生成github_cookie文件,用cookies保存的信息加载个人设置,验证登录是否成功源代码)
会话设置
self.session=requests.session (
生成github_cookie文件
self.session.cookies=cookie lib.lwpcookiejar (文件名=' github _ cookie ' ) )
三、POST表格的提交 POST表格的提交字段的获取如下图所示。
源代码的对应部分:
登录时的表单提交参数
表单数据:
提交:登入
utf8:
: YYY原装4a GHZ0u7R25 YMZJISFT JGDDowd9FKHM0ou VHD1WJuhBN 2S W0 CZ1VGLZWD DGNO 543 JOD2M8 JWL V6 w==
登录: * * * * * *
密码: * * * * * * *
四、获取authenticity_token向浏览器输入https://github.com/login,获取网络文本文件
源代码的对应部分:
获取身份_工具包
defget _ token (自动) :
response=self.session.get (自我记录,头=自我记录)。
html=etree.html (响应.文本) ) ) ) )。
authenticity _ token=html.XPath (/div /输入(2)/@值) ) ) ) ) ) ) ) ) ) ) ) ) )。
打印(身份_工具包) )。
返回身份_工具包
五、个人中心的验证判断是否成功个人中心的验证判断是否成功,输出个人中心的信息后登录成功。 下图:
也就是说,模拟GitHub模拟登录成功了。
完整的源代码:
# #编码: utf-8
''''
Github登录过程模拟:
1、请求报头:self.headers、url
2、设定session,保存登录信息cookies,生成github_cookie文件
3、提交POST表格,请求数据格式post_data
4、获取authenticity_token
5、通过个人中心验证判断登录是否成功,输出个人中心信息后登录成功
''''
impor
t requests from lxml import etree try: import cookielib except: import http.cookiejar as cookielib class GithubLogin(): def __init__(self): # url self.loginUrl = 'https://github.com/login' self.postUrl = 'https://github.com/session' self.profileUrl = 'https://github.com/settings/profile' # 设置请求头 self.headers = { 'Referer': 'https://github.com/', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36', 'Host': 'github.com' } # 设置session self.session = requests.session() # 生成github_cookie文件 self.session.cookies = cookielib.LWPCookieJar(filename='github_cookie') ''' 登陆时表单提交参数 Form Data: commit:Sign in utf8:✓ authenticity_token:yyZprIm4aghZ0u7r25ymZjisfTjGdUAdDowD9fKHM0oUvHD1WjUHbn2sW0Cz1VglZWdGno543jod2M8+jwLv6w== login:***** password:****** ''' def post_account(self, email, password): post_data = { 'commit': 'Sign in', 'utf8': '✓', 'authenticity_token': self.get_token()[0], 'login': email, 'password': password } response = self.session.post(self.postUrl, data=post_data, headers=self.headers) # 保存cookies self.session.cookies.save() def load_cookie(self): try: self.session.cookies.load(ignore_discard=True) except: print('cookie 获取不成功') # 获取authenticity_token def get_token(self): response = self.session.get(self.loginUrl, headers=self.headers) html = etree.HTML(response.text) authenticity_token = html.xpath('//div/input[2]/@value') print(authenticity_token) return authenticity_token # 判断是否登陆成功 def isLogin(self): self.load_cookie() response = self.session.get(self.profileUrl, headers=self.headers) selector = etree.HTML(response.text) flag = selector.xpath('//div[@class="column two-thirds"]/dl/dt/label/text()') info = selector.xpath('//div[@class="column two-thirds"]/dl/dd/input/@value') textarea = selector.xpath('//div[@class="column two-thirds"]/dl/dd/textarea/text()') # 登陆成功返回来的个人设置信息 print(u'个人设置Profile标题: %s'%flag) print(u'个人设置Profile内容: %s'%info) print(u'个人设置Profile内容: %s'%textarea) if __name__ == "__main__": github = GithubLogin() # 输入自己email账号和密码 github.post_account(email='******', password='******') # 验证是否登陆成功 github.isLogin()