跳到正文

科技·工程

当你写了个 bug 却刚好能够运行(6)

_Spectator_约 4 分钟
原文
文章信息与版本

文章信息

发布
2025年5月5日
收录
2025年12月12日
更新
2026年9月9日
篇幅
约 4 分钟

版本

  1. v1当前
WARNING

本文仅作个人学习记录,不建议在不了解程序具体功能的情况下擅自运行使用。请务必遵守洛谷相关用户协议,不得使用自动化工具干扰洛谷正常运行。

洛谷打卡机。

这次又是 Python 了。

可以打卡 + 截图。

保存为 .pyw 就可以隐藏命令行。

需在 config.json 中用 json 的格式填写保存截图的位置和 cookie。

Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.options import Options
import time, json

while True:
    try:
        now = time.localtime(time.time())
        sec = time.strftime("%M%S", now)
        date = time.strftime("%Y-%m-%d", now)
        if sec != "3000": time.sleep(0.5); continue
        options = Options(); options.add_argument("--headless=new")
        with webdriver.Edge(options = options) as driver:
            driver.maximize_window(); driver.get('https://www.luogu.com.cn')
            with open('config.json', 'r') as config:
                data = json.load(config)
                for uid, cid in data['cookies'].items():
                    driver.delete_all_cookies()
                    driver.add_cookie({'name': '_uid', 'value': str(uid)})
                    driver.add_cookie({'name': '__client_id', 'value': cid})
                    driver.refresh()
                    button = driver.find_elements(By.NAME, 'punch')
                    if not button: continue
                    button[0].click(); time.sleep(2)
                    res = driver.find_element(By.CLASS_NAME, "lg-punch")
                    res.screenshot(f"{data['save_path']}/{date}_{uid}.png")
    except Exception as e: pass

config.json 格式:

JSON
{
    "save_path": "F:/??/??",
    "cookies": {
        "52??41": "????b63e0ffc81b2681c536cbbe530279e37ddd1",
        "57??52": "????4b6cd9453041682be1238acec4cafe04b81f",
        "78??43": "????a9e6cd368f17682be0772e0892a641d43ac7"
    }
}
原文