This commit is contained in:
Luthics 2023-09-16 15:54:53 +08:00
commit 9264bb2b1c
7 changed files with 263 additions and 0 deletions

2
.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
test.db
/ref/__pycache__/*.pyc

50
README.md Normal file
View File

@ -0,0 +1,50 @@
[涉及到的一些内容](./tech.md)
## 简述
你需要提交一个使用 `Python` 制作的后端程序,包含以下功能
+ 连接到本地的 `SQLite` 数据库来管理数据
+ 通过 `Flask` 框架搭建一个 `HTTP` 服务器来处理 `GET` 请求
+ 通过简单的爬虫来获取指定网站的数据
+ 将整个代码库提交到 `git` 托管网站上(⚠请创建私有代码库以确保代码安全)
+ (可选) 用面向对象的思想拆分各个模块来提高代码的可读性
+ (可选) 提供筛选查找功能(详见下方的 3.
## 详情
1. 写一个爬虫来爬取教务处首页http://dean.xjtu.edu.cn/jxxx/xytz.htm 学业通知栏目中的通知列表,并将其存储在 本地的 `SQLite` 数据库 `data.db`
![1694848053023.png](https://picture-1300689095.file.myqcloud.com/2023/09/16/65055436b7dff.png)
2. 使用 `Flask` 框架展示爬取到的数据,在`GET /` 时,通过 `render_template` 将**指定格式**的数据传递到模板 `index.html` 中展示
3. (可选拓展)使用 `Flask` 框架提供筛选查找功能,在 `POST /data` 的时候,取出请求中传递的 `date`,并通过 `date` 字段来筛选数据,将筛选后的数据传递到模板 `index.html` 中展示
## 更多说明
1. **指定格式的**数据
```
[
{
"title": "关于做好推荐2024年优秀应届本科毕业生免试攻读研究生工作的通知",
# title 表示通知的标题
"date": "2023-09-15",
# date 表示通知发表的日期
"link": "/info/1013/12030.htm",
# link 表示点击后跳转的链接
},
...
# 很多个相同类型的数据
]
```
2. 数据库参考格式:
| id | title | date | link |
| --- | ------------------------------------------------------------ | ---------- | -------------------- |
| 1 | 关于做好推荐2024年优秀应届本科毕业生免试攻读研究生工作的通知 | 2023-09-15 | /info/1013/12030.htm |
| 2 | ... | ... | ... |
3. 文件夹 `ref` 中包含了一些参考代码,你可以参考其中的代码来完成任务
4. 用于渲染数据的模板 `index.html``ref/templates` 文件夹中,简单修改后即可使用
5. 截止时间为 2023.09.23 23:59:59

73
ref/db.py Normal file
View File

@ -0,0 +1,73 @@
import sqlite3
# 创建或者链接数据库
conn = sqlite3.connect('test.db')
# 创建游标
c = conn.cursor()
#########################
# 数据库创建表 datas
# id, name, date
sql = '''
CREATE TABLE IF NOT EXISTS datas (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name VARCHAR(255),
date VARCHAR(255)
)
'''
c.execute(sql)
#########################
def get_datas():
sql = '''
SELECT * FROM datas
'''
c.execute(sql)
datas = c.fetchall()
print(datas)
#########################
# 插入数据
sql = '''
INSERT INTO datas (name, date) VALUES ('张三', '2019-01-01')
'''
c.execute(sql)
conn.commit()
get_datas()
#########################
# 修改数据
sql = '''
UPDATE datas SET name='李四' WHERE id=1
'''
c.execute(sql)
conn.commit()
get_datas()
#########################
# 删除数据
sql = '''
DELETE FROM datas WHERE id=1
'''
c.execute(sql)
conn.commit()
get_datas()

18
ref/spider.py Normal file
View File

@ -0,0 +1,18 @@
import requests
import re
import bs4
x = requests.get('http://gs.xjtu.edu.cn/index.htm')
# <li>
# <span class="time fr">2023-09-12</span>
# <a class="tzgglm" href="tzgg/pygz.htm">培养工作</a>|<a class="tzggbt" href="info/1146/9867.htm" title="关于2023年下半年全国大学英语四、六级考试报名的通知"><span style="color: red; --darkreader-inline-color: #ff1a1a;" data-darkreader-inline-color="">[置顶]</span>关于2023年下半年全国大学英语四、六级考试报名的通知</a>
# </li>
# 用正则获取其中的 time
data_time = re.findall('<span class="time fr">(.*?)</span>', x.text)
# 用 bs4 获取其中的 time
soup = bs4.BeautifulSoup(x.text, 'html.parser')
data_time = soup.find_all('span', class_='time fr')

37
ref/templates/index.html Normal file
View File

@ -0,0 +1,37 @@
<!DOCTYPE html>
<html>
<head>
<title>数据展示</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://api.yztv.live/cdn/output.css">
</head>
<body class="bg-gray-100 text-gray-800" style="padding: 0;">
<div class="container mx-auto px-4 sm:px-6 lg:px-8">
<h1 class="text-3xl font-bold mb-4 mt-8">Data List</h1>
<div class="w-full text-left">
<table class="table-auto w-full border-collapse">
<thead>
<tr class="flex flex-row">
<th class="flex-1 px-4 py-2 border border-gray-200 bg-gray-200">姓名</th>
<th class="px-4 py-2 border border-gray-200 bg-gray-200 table-cell" style="width: 130px;">日期
</th>
</tr>
</thead>
<tbody>
{% for item in data %}
<tr class="flex flex-row">
<td class="flex-1 flex-wrap border border-gray-200 px-4 py-2 whitespace-pre-wrap" style="height: 48px;">{{ item.name }}
</td>
<td class="flex-shrink-0 border border-gray-200 px-4 py-2 whitespace-pre-wrap table-cell"
style="width: 130px; height: 48px;">{{ item.date }}</td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
</div>
</body>
</html>

25
ref/web.py Normal file
View File

@ -0,0 +1,25 @@
from flask import Flask, render_template
app = Flask(__name__)
@app.route('/')
def index():
datas = [{
'name': '张三',
'date': '2019-01-01'
}, {
'name': '李四',
'date': '2019-01-02'
}, {
'name': '王五',
'date': '2019-01-03'
}, {
'name': '赵六',
'date': '2019-01-04'
}]
return render_template('index.html', data=datas)
if __name__ == '__main__':
app.run(debug=True)

58
tech.md Normal file
View File

@ -0,0 +1,58 @@
# Backend Guide
## Git
### 介绍
`git` 是一个分布式版本控制软件,可以用它来管理文件的变动,方便追踪版本历史记录
### 要求
+ 会通过 `git` 简单的管理代码
+ 会连接到远程 `git` 仓库并保持同步
### 参考链接
+ [Git 教程 | 廖雪峰的官方网站](https://www.liaoxuefeng.com/wiki/896043488029600 "Git 教程 | 廖雪峰的官方网站")
+ [Git 教程 | 菜鸟教程](https://m.runoob.com/git/git-basic-operations.html "Git 教程 | 菜鸟教程")
### Test
1. 在任意 `git` 托管网站建立一个存储库
2. 将在本地新建的 `git` 存储库和 `git` 托管网站上的远程库同步
3. 将任意文件同步到 `git` 托管网站
### 推荐的存储库列表
+ [我的自建Git](https://git.luthics.com/) 推荐注册,使用 `Gitea` 搭建,会是近期内工作的主要平添
+ [Github](https://github.com/) 最有知名度的一个,国内网络环境下可能出现连接不稳定
+ [Gitee](https://gitee.com/) 国内最知名的,公开库有代码审查,用起来不太方便
## Python
### 要求
+ 基础的 `Python` 语法
+ 基础的面向对象知识
+ 会通过库管理器 `pip` 安装第三方库
## SQL
### 要求
+ 掌握数据库基础知识
+ 会写简单的 `SQL` 语句
+ 会通过 `Python` 连接到数据库并管理数据库
## HTML
### 要求
+ 了解 `HTTP` 基础知识
+ 了解 `HTML` 的基本组成元素
+ 会写简单的网页
+ 掌握网页的简单调试方法(`F12`)
+ 了解 `session`, `cookie` 等概念
## Flask [Python]
### 要求
+ 了解 `Flask` 框架基础知识
+ 会用 `Flask` 框架撰写简单的 `HTTP` 服务器
## 爬虫 Requests & BeautifulSoup [Python]
### 要求
+ 会通过 requests 库发送各类请求
+ 会通过 bs4 快速寻找 html 中的元素
+ 了解简单的正则
## Markdown
### 要求
+ 了解基本格式
+ 掌握基本的 `API` 文档格式