• 接龙管家-Python自动打卡(续)获取接龙管家token


    获取接龙管家token

    前言

    之前写了接龙管家的python打卡,但是它的token有效期只有三四天,所以需要经常更换

    最后采取的方法是:

    1. Selenium 模拟登录网页版接龙管家
    2. 获取登录小程序码用邮件发送到手机
    3. 扫码登录
    4. 将token存储在本地
    5. 读取token运行打卡脚本

    条件

    需要的条件是有云服务器(或者本地不关机运行也行)、然后安装配置python、requests、selenium、chrome、chromedriver等

    代码

    最后附上代码:

    from selenium import webdriver#核心
    import requests#核心
    import time#用来显示打卡日期
    import yagmail#用来发送登录码、打卡信息,不用也可以
    import os#用来在脚本里运行另一个脚本
    import json#打卡脚本里用的,这里好像没用
    
    print(time.strftime("========= %Y年%m月%d日 =========== refresh",time.gmtime()))
    
    option = webdriver.ChromeOptions()
    option.add_argument('--headless')
    option.add_argument('no-sandbox')
    option.add_argument('disable-dev-shm-usage')
    driver = webdriver.Chrome(chrome_options=option)
    
    driver.get('https://jielong.co/')
    print(driver.title)
    
    #设置位置之后的所有元素定位操作都有最大等待时间十秒,在10秒内会定期进行元素定位,超过设置时间之后将会报错
    driver.implicitly_wait(10)
    
    #__RequestVerificationToken
    RequestVerificationToken = driver.find_element_by_name('__RequestVerificationToken').get_attribute('value')
    print('RequestVerificationToken: ',RequestVerificationToken)
    
    #点击
    #//*[@id="section1"]/div/div[2]/a
    #/html/body/div/div[1]/div/div[2]/a
    driver.find_element_by_xpath('//a[@class="code-btn code-login"]').click()
    time.sleep(1)
    
    #url https://jielong.co/Portal/GetWXQRCode?key=gnxqlx0fr542tpfg31iudaxu&returnUrl=
    #//*[@id="QRContent"]/img
    url = driver.find_element_by_xpath('//*[@id="QRContent"]/img').get_attribute('src')
    print('url: ',url)
    #key
    key = url[ url.find( '=' )+1 : url.find( '&' ) ]
    print('key: ',key)
    
    #获取登录小程序码
    session = requests.session()
    #用requests.session()创建session对象,相当于创建了一个空的会话框,准备保持cookies。
    headers = {
        'Host': 'jielong.co',
        'Connection': 'keep-alive',
        'sec-ch-ua': '" Not A;Brand";v="99", "Chromium";v="102", "Google Chrome";v="102"',
        'sec-ch-ua-mobile': '?0',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/102.0.5005.63 Safari/537.36',
        'sec-ch-ua-platform': "Windows",
        'Accept': 'image/avif,image/webp,image/apng,image/svg+xml,image/*,*/*;q=0.8',
        'Sec-Fetch-Site': 'same-origin',
        'Sec-Fetch-Mode': 'no-cors',
        'Sec-Fetch-Dest': 'image',
        'Referer': 'https://jielong.co/',
        'Accept-Encoding': 'gzip, deflate, br',
        'Accept-Language': 'zh-CN,zh;q=0.9',
        'Cookie': '__RequestVerificationToken=' + RequestVerificationToken
    }
    
    img = session.get(url,headers=headers).content
    
    #保存图片到本地
    with open('login.jpg', 'wb') as file:
        file.write(img)
        
    #发送图片
    yag=yagmail.SMTP(user='...@qq.com',password='...',host='smtp.qq.com',encoding='GBK')
    yag.send(to='...@qq.com',subject='快来扫登录码',contents=[yagmail.inline("./login.jpg")])
    print('邮件发送成功')   
    yag.close()
        
    #等待扫码
    
        #读取token
    with open("/home/jielongguanjia/token.txt", "r") as f:
        old_token = f.read()
        print('old_token: ', old_token)
    token = old_token
    for i in range(100):
        print('等待扫码 ',i)
        # print('driver.get_cookies(): ', driver.get_cookies())
        for item in driver.get_cookies():
            if item['name'] == 'token':
                token = item['value']
                print(token)
                break
        if token != old_token: #当token未过期的时候,兑换的token没有变,但是这里的token有Bearer%20,所以不等
            break
        time.sleep(6)
    
    #扫码失败
    if token == old_token:
        print('获取token失败!')
        yag=yagmail.SMTP(user='...@qq.com',password='...',host='smtp.qq.com',encoding='GBK')
        yag.send(to='...@qq.com',subject='获取token失败',contents='获取token失败')
        print('获取token失败-邮件发送成功')
        yag.close()
        driver.quit()
        os._exit(0)
        
    #获取token
    token = token.replace('Bearer%20','')
    print('token: ', token)
    #设置token
    with open("/home/jielongguanjia/token.txt","w") as f:
        f.write(token)  # 自带文件关闭功能,不需要再写f.close()
    print('重写token成功!')
    time.sleep(1)
    
    driver.quit()
    
    #重新运行打卡脚本--打卡脚本里也可以在判断需要登录时,自动运行获取token的脚本
    # res = os.popen("python3 /home/jielongguanjia/jielong2.py")    #运行命令 
    # print('运行打卡脚本!')
    # print(res.read())
    # res.close()
    
    #print('----------------------------------------------------------------------')
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119

    碎碎念

    刚开始就是手动抓包复制粘贴token,后来觉得太麻烦了,然后尝试了几种方法:
    1.搭建一个flask网页,将复制的token直接发送到云服务器,就不需要打开云服务器了;
    2.抓包还是太麻烦,然后上网页版看了看,有token可用,然后就复制粘贴网页版的token;
    3.打开网页还是太麻烦,既然网页上有,直接python爬取试试,然后用Selenium 模拟登录,获取登录小程序码存储到本地,然后手机扫码,最后成功获取到token
    4.扫码还是太麻烦,如果能token换token就好了,然而并不可以,我尝试了抓授权操作的包,模拟授权,但是在token未过期时,授权状态是正常的,token过期后还是不行

    最后采用的就是文章里的方法,如果有人知道怎么跳过扫码这一步,请告诉我,不胜感激!

  • 相关阅读:
    arcgis中坡向计算工作原理说明
    Uniapp 婚庆服务全套模板前端
    美团2024届秋招笔试第一场编程【小美走公路】
    FWT/快速沃尔什变换 入门指南
    Docker技术入门| Part02:Docker基础用法(Docker镜像使用、操作容器、数据管理)
    『虫无涯→_→读书推荐02期』|全面系统的〖Effective软件测试〗带你完成所有不同类型的测试,GO
    【C++从0到王者】C++11(全文三万字,超详解)
    软件企业需要每年年审吗?
    Android Studio新建项目下载依赖慢,只需一个操作解决
    【jQuery Demo】图片瀑布流实现
  • 原文地址:https://blog.csdn.net/tfnmdmx/article/details/126707684