跳到正文

科技·工程

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

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

文章信息

收录
2025年12月12日
更新
2026年8月9日
篇幅
约 3 分钟

版本

  1. v1当前

洛谷提交记录爬虫。

这一版是为 Garbage_fish 设计的,其他人可以自己对着代码改改。

查看提交记录需要登录,这里用的是 cookie 登录,如果 cookie 失效可以换个 cookie 填到代码里。

你需要:

不懂的可以问 AI。

Python
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.edge.service import Service
import os, time

print('启动中,请稍后。。')
service = Service(service_log_path=None)
driver = webdriver.Edge(service=service)
driver.get('https://www.luogu.com.cn')
driver.delete_all_cookies()
driver.add_cookie({'name': '_uid', 'value': '1049329'})
driver.add_cookie({'name': '__client_id', 'value': 'd64bb845d0cbe6d6682d7a65a62cf92a93ed3bb3'})
url = 'https://www.luogu.com.cn/record/list'
end = 'https://www.luogu.com.cn/record/201381793'
problem_set = set()
for page in range(1, 100):
    driver.get(f'{url}?user=garbage_fish&status=12&page={page}')
    brk = False; time.sleep(2)
    records = driver.find_elements(By.CLASS_NAME, 'row')
    for record in records:
        status = record.find_element(By.CLASS_NAME, 'status')
        problem = record.find_element(By.CLASS_NAME, 'problem')
        rid = status.find_element(By.TAG_NAME, 'a').get_attribute('href')
        pid = problem.find_element(By.CLASS_NAME, 'pid').text
        print(f'找到一条记录:rid = {rid[32:]}, pid = {pid}')
        problem_set.add(pid)
        if rid == end: brk = True; break
    if brk: print('爬取结束。'); break
print(problem_set)
driver.quit()
原文