github模拟登陆

github模拟登陆

  • 使用requests,xpath模拟GitHub登陆

  • 具体步骤思路如下:

    • 清空浏览器保存的GitHub网站的cookies

    • 回到登陆界面登陆,F12开发者模式,分析参数

      • 点击登陆,便可在开发者工具中看到各种请求,点开

      • 可以看到请求地址为 https://github.com/session,请求方式为POST

      • 在这里插入图片描述

      • 分析提交表单,可以看到 authenticity_token、ga_id、timestamp、timestamp_secret等参数无法自行构造

      • 在这里插入图片描述

      • 因此,重新清空浏览器cookies,返回到登陆界面,截获部分发送请求

      -在这里插入图片描述

      • 查看login的源代码,全局搜索上述参数,因此知道 https://github.com/login 源代码中包含了上述参数。现在已经知道了所有信息的来源,接下来代码实现模拟登陆。

      -在这里插入图片描述

      • `

        import requests
        from lxml import etree
        
        class Login(object):
           def __init__(self):
              self.headers={
                 "Referer":"https://github.com/",
                 "Usser-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/84.0.4147.89 Safari/537.36",
                 "Host":"github.com"
              }
              self.login_url="https://github.com/login"
              self.post_url="https://github.com/session"
              self.session=requests.Session()
        
           def token(self):
              response = self.session.get(self.login_url,headers=self.headers)
              selector=etree.HTML(response.text)
              li=selector.xpath("//div//input//@value")
              token=li[0] #authenticity_token
              timestamp=li[3]
              timestamp_secret=li[4]
              return [token,timestamp,timestamp_secret]
           
           def login(self,email,passwd):
              li=self.token()
              post_data={
                 "commit":"Sign in",
                 "authenticity_token":li[0],
                 "ga_id":"909892832.1595567052",
                 "login":email,
                 "password":passwd,
                 "webauthn-support":"supported",
                 "webauthn-iuvpaa-support":"suppotred",
                 "return_to":"",
                 "required_field_120e":"",
                 "timestamp":li[1],
                 "timestamp_secret":li[2]
              }
              response=self.session.post(self.post_url,data=post_data,headers=self.headers)
              if response.status_code==200:
                 self.dynamics(response.text)
                 
           #抓取public信息证明成功登陆
           def dynamics(self,html):
              selector=etree.HTML(html)
              dynamics=selector.xpath("//div[@class='news']//ul/li[@class='public source ']//a/@href")
              print(dynamics)
                 
        
        if __name__ == '__main__':
           login=Login()
           login.login(email="xxxxx",passwd="xxxxxxx")
        
      • 综上实现GitHub模拟登陆

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值