commit 9264bb2b1cc0c14a3f227a8e1375f634d4f5e6fc Author: Luthics Date: Sat Sep 16 15:54:53 2023 +0800 init diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d43896e --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +test.db +/ref/__pycache__/*.pyc \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..00eb559 --- /dev/null +++ b/README.md @@ -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 \ No newline at end of file diff --git a/ref/db.py b/ref/db.py new file mode 100644 index 0000000..00caa97 --- /dev/null +++ b/ref/db.py @@ -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() diff --git a/ref/spider.py b/ref/spider.py new file mode 100644 index 0000000..9d286ab --- /dev/null +++ b/ref/spider.py @@ -0,0 +1,18 @@ +import requests +import re +import bs4 + +x = requests.get('http://gs.xjtu.edu.cn/index.htm') + +#
  • +# 2023-09-12 +# 培养工作|[置顶]关于2023年下半年全国大学英语四、六级考试报名的通知 +#
  • + +# 用正则获取其中的 time +data_time = re.findall('(.*?)', x.text) + + +# 用 bs4 获取其中的 time +soup = bs4.BeautifulSoup(x.text, 'html.parser') +data_time = soup.find_all('span', class_='time fr') diff --git a/ref/templates/index.html b/ref/templates/index.html new file mode 100644 index 0000000..14d6c85 --- /dev/null +++ b/ref/templates/index.html @@ -0,0 +1,37 @@ + + + + + 数据展示 + + + + + +
    +

    Data List

    +
    + + + + + + + + + {% for item in data %} + + + + + {% endfor %} + +
    姓名日期 +
    {{ item.name }} + {{ item.date }}
    +
    +
    + + + \ No newline at end of file diff --git a/ref/web.py b/ref/web.py new file mode 100644 index 0000000..f7a73e5 --- /dev/null +++ b/ref/web.py @@ -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) diff --git a/tech.md b/tech.md new file mode 100644 index 0000000..ba412ab --- /dev/null +++ b/tech.md @@ -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` 文档格式 \ No newline at end of file