关于洛谷登录脚本
  • 板块灌水区
  • 楼主zengyukai2012
  • 当前回复1
  • 已保存回复1
  • 发布时间2024/10/2 15:54
  • 上次更新2024/10/4 17:11:21
查看原帖
关于洛谷登录脚本
1090444
zengyukai2012楼主2024/10/2 15:54
import re
import time
import requests
import json

cookies = {}  # 将整数转换为字符串

import requests


def login(username, password, captcha):
    url = "https://www.luogu.com.cn/do-auth/password"
    data = {"username": username, "password": password, "captcha": captcha}

    try:
        response = requests.post(
            url,
            json=data,
            headers={
                "accept": "application/json, text/plain, */*",
                "accept-encoding": "gzip, deflate, br, zstd",
                "accept-language": "zh-CN,zh;q=0.9",
                "cache-control": "no-cache",
                "content-type": "application/json",
                "cookie": "__client_id=dc2150c66bd63330201a52e07aec10a86fa0e70b; _uid=0",
                "origin": "https://www.luogu.com.cn",
                "pragma": "no-cache",
                "priority": "u=1, i",
                "referer": "https://www.luogu.com.cn/auth/login",
                "sec-ch-ua": '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
                "sec-ch-ua-mobile": "?0",
                "sec-ch-ua-platform": '"Windows"',
                "sec-fetch-dest": "empty",
                "sec-fetch-mode": "cors",
                "sec-fetch-site": "same-origin",
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
                "x-csrf-token": "1727940475:nNyiLYCyMH+uwn97yftfOTqCmmgdxo9wx5ryXfQipC8=",
                "x-requested-with": "XMLHttpRequest",
            },
        )
        print(json.loads(response.text))
        print(response.headers)
        return response
    except Exception as e:
        print(f"发生错误: {e}")
        return None


def get_captcha():
    url = "https://www.luogu.com.cn/api/verify/captcha"
    try:
        response = requests.get(
            url,
            headers={
                "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
                "accept-encoding": "gzip, deflate, br, zstd",
                "accept-language": "zh-CN,zh;q=0.9",
                "cache-control": "no-cache",
                "pragma": "no-cache",
                "priority": "u=0, i",
                "sec-ch-ua": '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
                "sec-ch-ua-mobile": "?0",
                "sec-fetch-dest": "document",
                "sec-fetch-mode": "navigate",
                "sec-fetch-site": "none",
                "sec-fetch-user": "?1",
                "upgrade-insecure-requests": "1",
                "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
            },
            cookies=cookies,
        )
        response.raise_for_status()  # 检查请求是否成功
        with open("captcha.png", "wb") as f:
            f.write(response.content)
        return input("请输入验证码:")
    except requests.exceptions.RequestException as e:
        print(f"获取验证码时出现错误: {e}")
        return None


# 示例调用
if __name__ == "__main__":
    response = requests.get(
        "https://www.luogu.com.cn",
        headers={
            "accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
            "accept-encoding": "gzip, deflate, br, zstd",
            "accept-language": "zh-CN,zh;q=0.9",
            "cache-control": "no-cache",
            "pragma": "no-cache",
            "priority": "u=0, i",
            "sec-ch-ua": '"Microsoft Edge";v="129", "Not=A?Brand";v="8", "Chromium";v="129"',
            "sec-ch-ua-mobile": "?0",
            "sec-fetch-dest": "document",
            "sec-fetch-mode": "navigate",
            "sec-fetch-site": "none",
            "sec-fetch-user": "?1",
            "upgrade-insecure-requests": "1",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36 Edg/129.0.0.0",
        },
        cookies=cookies,
    )
    cookie_part = response.headers["set-cookie"]
    client_id_match = re.search(r"__client_id=([^;]+)", cookie_part)
    uid_match = re.search(r"_uid=([^;]+)", cookie_part)

    client_id_match = client_id_match.group(1) if client_id_match else None
    uid_match = uid_match.group(1) if uid_match else None

    cookies["__client_id"] = client_id_match
    cookies["_uid"] = uid_match

    print(cookies)

    captcha_input = get_captcha()
    if captcha_input:  # 检查验证码输入是否有效
        response_text = login("【用户名】", "【密码】", captcha_input)
        print(response_text)

为什么总是提示“图形验证码错误”?

2024/10/2 15:54
加载中...