Python模拟Github登陆

Python模拟Github登陆,详情请查看源码点链接进入Python-Spiders文集,模拟Github登陆可以分为五个操作步骤,步骤如下:

模拟Github登陆步骤:
    1、请求头:self.headers,请求url;
    2、设置session,保存登陆信息cookies,生成github_cookie文件;
    3、POST表单提交,请求数据格式post_data;
    4、authenticity_token获取;
    5、在个人中心验证判断是否登陆成功,输出个人中心信息即登陆成功。

一、获取请求头
① 在浏览器中敲入https://github.com/login,同时右击页面查看检查,如下图所示:

github_login1.png

② 点击红框内login进入如下图所示:
github_login2.png

③ 源码中对应部分:

# 设置请求头
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'
}

二、保存登陆信息cookies
① 设置session
② 保存登陆信息cookies,生成github_cookie文件,用cookies保存的信息加载个人设置,验证是否模拟登录成功
③ 源码中对应部分:

 # 设置session
self.session = requests.session()
# 生成github_cookie文件
self.session.cookies = cookielib.LWPCookieJar(filename='github_cookie')

三、POST表单提交
① POST表单提交字段获取,如下图所示:


github_login3.png

② 源码中对应部分:

登陆时表单提交参数
Form Data:
     commit:Sign in
     utf8:✓
     authenticity_token:yyZprIm4aghZ0u7r25ymZjisfTjGdUAdDowD9fKHM0oUvHD1WjUHbn2sW0Cz1VglZWdGno543jod2M8+jwLv6w==
     login:*****
     password:******
    

四、authenticity_token获取
① 在浏览器中敲入https://github.com/login,获取网页文本文件

② 源码中对应部分:

# 获取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

五、在个人中心验证判断是否登陆成功
在个人中心验证判断是否登陆成功,输出个人中心信息即登陆成功,如下图:


github_login4.png

即模拟GitHub模拟登陆成功。

注意:Python-Spiders文集中收录了较多的爬取文件练习资料,后期也会陆续更新新的Python-Spiders学习资料,共同学习,一起进步,喜欢的小伙伴请Star和Fork哦,对Python-Spiders不懂的小伙伴可以私信我哦,探讨和学习。
附源码:

# encoding: utf-8
'''
模拟Github登陆步骤:
    1、请求头:self.headers,请求url
    2、设置session,保存登陆信息cookies,生成github_cookie文件
    3、POST表单提交,请求数据格式post_data
    4、authenticity_token获取
    5、在个人中心验证判断是否登陆成功,输出个人中心信息即登陆成功

'''

import 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()
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • Android 自定义View的各种姿势1 Activity的显示之ViewRootImpl详解 Activity...
    passiontim阅读 177,231评论 25 709
  • 太长了,还是转载吧...今天在看博客的时候,无意中发现了@Trinea在GitHub上的一个项目Android开源...
    庞哈哈哈12138阅读 20,361评论 3 283
  • 离离原上草,一岁一枯荣。野火烧不尽,春风吹又生。 远芳侵古道,晴翠接荒城。又送王孙去,萋萋满别情。 这一首《赋得古...
    长风18阅读 3,529评论 0 0
  • 此处梳理出面向人工智能的机器学习方法体系,主要体现机器学习方法和逻辑关系,理清机器学习脉络,后续文章会针对机器学习...
    zelo阅读 2,472评论 0 0
  • 很多时候听到别人长篇大论絮絮叨叨的说大道理时,没由来的有点不悦的小情绪,这是啥情况呢?按道理每个人经历过无数的沟沟...
    动车阅读 1,805评论 0 3

友情链接更多精彩内容