当你写了个 bug 却刚好能够运行(7)
_Spectator_ · · 科技·工程
洛谷提交记录爬虫。
这一版是为 Garbage_fish 设计的,其他人可以自己对着代码改改。
查看提交记录需要登录,这里用的是 cookie 登录,如果 cookie 失效可以换个 cookie 填到代码里。
你需要:
- Python3(https://www.python.org/downloads/release/python-3133/)
- selenium 库(
pip install selenium) - Microsoft Edge Webdriver(也可以换成其他浏览器,自己改代码)
不懂的可以问 AI。
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()