jzy 提出可以搞一个 bingo,我来实现一下
先把紫题都爬下来
import requests
from lxml import etree
url = 'https://www.luogu.com.cn/problem/list?difficulty=6&page='
n = 51
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36",
}
for i in range(1, n + 1):
res = requests.get(url + str(i), headers=headers)
res.encoding = 'utf-8'
ele = etree.HTML(res.text)
lst = ele.xpath('//li/text()')
with open(f"luogu.txt", "a", encoding='utf-8') as f:
for name in lst:
f.write(name[:-1] + ',')
print(f"page {i} done.")然后随机 25 个题
import random
lst = []
with open("luogu.txt", "r", encoding='utf-8') as f:
con = f.read()
name = con.split(',')
for i in name:
if i != '':
lst.append(i)
with open("cf.txt", "r", encoding='utf-8') as f:
con = f.read()
name = con.split(',')
for i in name:
if i != '':
lst.append(i)
with open("at.txt", "r", encoding='utf-8') as f:
con = f.read()
name = con.split(',')
for i in name:
if i != '':
lst.append(i)
prob = random.sample(lst, 25)
with open("prob.txt", "w", encoding='utf-8') as f:
for i in prob:
f.write(i)
f.write('\n')
后端随便搞一下
from django.shortcuts import render
from .settings import BASE_DIR
import os
def index(request):
name = []
with open(os.path.join(BASE_DIR, "data/prob.txt"), "r", encoding='utf-8') as f:
for i in range(25):
name.append(f.readline()[:-1])
lst = []
for i in range(5):
lst.append(name[i:i+5])
con = {"lst": lst}
return render(request, 'index.html', con)然后把界面和样式搞一下
<!DOCTYPE html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<title>OI Bingo</title>
<style>
body, html {
padding: 5px;
}
.left-card {
float: left;
width: 60%;
}
.right-card {
float: left;
width: 40%;
}
table {
border-collapse: collapse;
}
table, td {
border:2px solid black;
}
td {
padding: 15px;
text-align: center;
width: 100px;
height: 100px;
}
</style>
</head>
<body>
<div class="left-card">
<h1>Bingo</h1>
<table border="1">
{% for row in lst %}
<tr>
{% for name in row %}
<td>{{ name }}</td>
{% endfor %}
</tr>
{% endfor %}
</table>
</div>
<div class="right-card">
<h1>状态</h1>
<h3>无</h3>
<h1>用户</h1>
<h3>无</h3>
</div>
</body>
</html>然后状态的话就手动罢