跳到正文

科技·工程

工程开发记录

ケロシ约 6 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月12日
篇幅
约 6 分钟

版本

  1. v1当前

jzy 提出可以搞一个 bingo,我来实现一下

先把紫题都爬下来

Python
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 个题

Python
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')

后端随便搞一下

Python
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)

然后把界面和样式搞一下

HTML
<!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>

然后状态的话就手动罢

原文