diff --git a/app.py b/app.py index c0e42c8..75e6c03 100644 --- a/app.py +++ b/app.py @@ -1,8 +1,13 @@ """ 产品参数爬取系统 - 主程序 +v2.0.0 - 合并后台管理到单端口 + +端口: 19011 +前台: http://localhost:19011 +后台: http://localhost:19011/admin """ -from flask import Flask, jsonify, request +from flask import Flask, jsonify, request, render_template from flask_cors import CORS import json import os @@ -18,7 +23,7 @@ logging.basicConfig( ) logger = logging.getLogger(__name__) -app = Flask(__name__) +app = Flask(__name__, template_folder='templates') CORS(app) # 路径配置 @@ -59,20 +64,21 @@ def save_tasks(data): TASKS_FILE.write_text(json.dumps(data, ensure_ascii=False, indent=2), encoding='utf-8') -# ============ API 路由 ============ +# ============ 前台 API 路由 ============ @app.route('/') def index(): - """首页""" + """前台首页 - API说明""" return jsonify({ "name": "Product Crawler", - "version": "1.0.0", + "version": "2.0.0", "description": "产品参数爬取系统", "endpoints": { "products": "/api/products", "tasks": "/api/tasks", "spiders": "/api/spiders", - "run": "/api/run/" + "run": "/api/run/", + "admin": "/admin" } }) @@ -326,12 +332,60 @@ def api_export(): return jsonify({"error": "Unsupported format"}), 400 +# ============ 后台管理页面路由 ============ + +@app.route('/admin') +def admin_index(): + """后台首页""" + return render_template('index.html') + + +@app.route('/admin/products') +def admin_products(): + """后台 - 产品管理""" + return render_template('products.html') + + +@app.route('/admin/spiders') +def admin_spiders(): + """后台 - 爬虫管理""" + return render_template('spiders.html') + + +@app.route('/admin/tasks') +def admin_tasks(): + """后台 - 任务管理""" + return render_template('tasks.html') + + +@app.route('/admin/config') +def admin_config(): + """后台 - 配置管理""" + return render_template('config.html') + + +# ============ 后台管理 API(统计) ============ + +@app.route('/api/admin/stats') +def api_admin_stats(): + """后台统计信息""" + data = load_products() + tasks = load_tasks().get("tasks", []) + + return jsonify({ + "total_products": len(data.get("products", [])), + "last_update": data.get("last_update"), + "total_tasks": len(tasks), + "running_tasks": len([t for t in tasks if t.get("status") == "running"]) + }) + + if __name__ == '__main__': print("=" * 60) - print("产品参数爬取系统") + print("产品参数爬取系统 v2.0.0") print("=" * 60) print(f"API地址: http://localhost:19011") - print(f"后台管理: http://localhost:19012") + print(f"后台管理: http://localhost:19011/admin") print("=" * 60) app.run(host='0.0.0.0', port=19011, debug=True) \ No newline at end of file diff --git a/templates/config.html b/templates/config.html new file mode 100644 index 0000000..8bfa92f --- /dev/null +++ b/templates/config.html @@ -0,0 +1,102 @@ + + + + + + 系统配置 - 产品爬取系统 + + + + +
+ + + + +
+
+

系统配置

+

爬虫系统配置信息

+
+ +
+ +
+

服务配置

+
+
+
API端口
+
19011
+
+
+
后台端口
+
19012
+
+
+
+ + +
+

爬虫配置

+
+
+
请求超时
+
30秒
+
+
+
重试次数
+
3次
+
+
+
请求间隔
+
1秒
+
+
+
并发限制
+
3
+
+
+
+ + +
+

+ 使用说明 +

+
+

1. 进入「爬虫管理」页面,选择要运行的爬虫

+

2. 点击「运行」按钮开始爬取数据

+

3. 爬取完成后,在「产品数据」页面查看结果

+

4. 支持导出JSON/CSV格式的数据

+
+
+
+
+
+ + \ No newline at end of file diff --git a/templates/index.html b/templates/index.html new file mode 100644 index 0000000..6cc82dc --- /dev/null +++ b/templates/index.html @@ -0,0 +1,199 @@ + + + + + + 产品爬取系统 - 后台管理 + + + + + +
+ + + + +
+ +
+

仪表盘

+

产品参数爬取系统概览

+
+ + +
+
+
+
+

产品总数

+

-

+
+
+ +
+
+
+ +
+
+
+

爬虫数量

+

1

+
+
+ +
+
+
+ +
+
+
+

任务总数

+

-

+
+
+ +
+
+
+ +
+
+
+

最后更新

+

-

+
+
+ +
+
+
+
+ + + + + +
+
+

最近任务

+ 查看全部 +
+
+

加载中...

+
+
+
+
+ + + + \ No newline at end of file diff --git a/templates/products.html b/templates/products.html new file mode 100644 index 0000000..10ea2b3 --- /dev/null +++ b/templates/products.html @@ -0,0 +1,142 @@ + + + + + + 产品数据 - 产品爬取系统 + + + + + +
+ + + + +
+
+
+

产品数据

+

查看爬取的产品参数数据

+
+
+ + +
+
+ + +
+
+

加载中...

+
+
+
+
+ + + + \ No newline at end of file diff --git a/templates/spiders.html b/templates/spiders.html new file mode 100644 index 0000000..169ce13 --- /dev/null +++ b/templates/spiders.html @@ -0,0 +1,136 @@ + + + + + + 爬虫管理 - 产品爬取系统 + + + + + +
+ + + + +
+
+

爬虫管理

+

配置和运行爬虫任务

+
+ + +
+

加载中...

+
+
+
+ + + + \ No newline at end of file diff --git a/templates/tasks.html b/templates/tasks.html new file mode 100644 index 0000000..5452dbb --- /dev/null +++ b/templates/tasks.html @@ -0,0 +1,102 @@ + + + + + + 任务记录 - 产品爬取系统 + + + + +
+ + + + +
+
+

任务记录

+

查看爬虫任务执行历史

+
+ + +
+
+

加载中...

+
+
+
+
+ + + + \ No newline at end of file