zoukankan      html  css  js  c++  java
  • github免密登陆

    import requests
    import re
    
    # 一:先获取登陆页面,拿到authenticity_token:
    # 1 请求的url:https://github.com/login
    # 2 请求方法:GET
    # 3 请求头:
    #    User-Agent
    r1 = requests.get('https://github.com/login',
                      headers={
                          'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
                      },
                      )
    authenticity_token = re.findall('name="authenticity_token".*?value="(.*?)"', r1.text, re.S)[0]
    r1_cookies=r1.cookies.get_dict()
    print(authenticity_token)
    print(r1_cookies)
    # 二:提交表单数据完成登陆
    # 1 请求的url:https://github.com/session
    # 2 请求方法:POST
    # 3 请求头:
    #    Referer:https://github.com/
    #    User-Agent
    # 4 请求体
    # commit:Sign in
    # utf8:✓
    # authenticity_token:pFLyO9choCgUd6mm1AMP7BoeEQ613TRDe49MBREZ7EU7MKM7IELFgmyGfcKXS0hsaIiGJ8YlkTD5nwwV4tebig==
    # login:xxxxxxx@qq.com
    # password:xxxxxxx
    r2 = requests.post('https://github.com/session',
                       headers={
                           "Referer": "https://github.com/",
                           'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
                       },
                       cookies=r1_cookies,
                       data={
                           "commit": "Sign in",
                           'utf8': "",
                           "authenticity_token": authenticity_token,
                           "login": "xxxxxx@qq.com",
                           "password": "xxxxxxxx",
                       },
                       allow_redirects=False
                       )
    
    # print(r2.status_code)
    # print(r2.history)
    
    cookies=r2.cookies.get_dict()
    
    r3=requests.get('https://github.com/settings/emails',
                 headers={
                     "Referer": "https://github.com/",
                     'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36',
                 },
                 cookies=cookies)
    print('xxxxxxx@qq.com' in r3.text)     #返回true,测试成功
  • 相关阅读:
    高格-远程支持中的奇怪问题【15】
    关于er图的几个工具
    如何解决win10明明是管理员还要权限的问题
    判断日期天数
    谈一谈在公司两次压测我总结的思路
    vue学习之-----v-model数据双向绑定,自定义组件父子传参
    Js各种小技巧总结
    openlayers学习之-----核心类
    openlayers学习之-----把坐标点改为WKT格式的数据
    新书介绍 -- 《Redis核心原理与实践》
  • 原文地址:https://www.cnblogs.com/52-qq/p/8290197.html
Copyright © 2011-2022 走看看