P

Python爬虫破解验证码示例

admin 默认分类 2024-03-06
import requests
import time
import json
import base64
import ddddocr

def checkin(session, headers, ocr, captcha_url, checkin_url):
    while True:
        # 获取验证码
        captcha = session.post(captcha_url, headers=headers).json()
        
        # 获取验证码ID和验证码图片
        captcha_id = captcha.get("data").get("captchaId")
        base64_image = captcha.get("data").get("picPath")
        
        # 分割解码base64字符串
        head, context = base64_image.split(",")
        image_data = base64.b64decode(context)
        
        # 保存图片为文件并识别验证码
        with open("captcha.png",'wb') as f, open('captcha.png', 'rb') as fr:
            f.write(image_data)
            captcha_text = ocr.classification(fr.read())
         
        # 发送登录请求
        url = checkin_url + '?turnstile=&captchaId=' + captcha_id + '&captcha=' + captcha_text
        response = session.post(url, headers=headers).json()
        message = response.get('message')
        print(message)

        # 登录成功则退出循环
        if response.get('success'):
            print("Login successful!")
            break
        else:
            print("Login failed, try again...")
            time.sleep(5)

def main():
    login_url = 'https://uu.ci/api/user/login'
    captcha_url = 'https://uu.ci/api/captcha'
    checkin_url = 'https://uu.ci/api/user/checkin'
    
    headers = {
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/122.0.0.0 Safari/537.36',
        'Content-Type': 'application/json'
    }
    data = {
        "username": "airpods",
        "password": "uuysyhl9T!"
    }
    
    ocr = ddddocr.DdddOcr()
    
    # Start session and login
    with requests.Session() as session:
        session.post(login_url, headers = headers, data = json.dumps(data))
        checkin(session, headers, ocr, captcha_url, checkin_url)
    
if __name__ == "__main__":
    main()

评论(0)

发布评论