洛谷文章广场搜索器
游记和娱乐版块不搜
效率不高,请耐心等待
upd on 2025/5/29:洛谷官方上线了文章搜索功能,现在没用了。
import requests
import re
from bs4 import BeautifulSoup
txt = input('搜索:').lower().replace(" ", "")
cnt = 0
for T in ["?", "?category=4&", "?category=3&"]:
s = set()
for page in range(1, 0x114514):
head = {"User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.130 Safari/537.36"}
response = requests.get(url=f"https://www.luogu.com.cn/article{T}page={page}",headers=head)
soup = BeautifulSoup(response.text, 'lxml')
links = soup.find_all('a')
end = 0
for a in links:
url = a.get('href')
text = a.get_text()
if url is None: continue
if re.match(r'[a-z0-9]{8}', url[-8:]) is None: continue
if url in s: end = 1; break
s.add(url)
if not txt in text.lower().replace(" ", ""): continue
print(f"https://www.luogu.com.cn{url}")
print(text, '\n')
cnt += 1
if end: break
print(f"找到 {cnt} 个结果")温馨提示:频繁使用会被洛谷 403 Forbidden。。。