diff --git a/QSBK.py b/QSBK.py index 42e9e4f..4496c77 100644 --- a/QSBK.py +++ b/QSBK.py @@ -1,91 +1,217 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- -__author__='WYY' -__date__='2017.03.13' - -#实战小项目:糗事百科—我的第一个小爬虫 import re -import requests import time -import codecs +import requests +from pathlib import Path +from requests.exceptions import RequestException, ConnectionError, Timeout + +__author__ = 'WYY' +__date__ = '2024.10.30' # 更新为当前优化时间 + + +class QiubaiSpider: + """糗事百科爬虫,支持多页数据爬取、格式化输出与文件保存""" + # 基础配置 + BASE_URL = "https://www.qiushibaike.com" + SAVE_DIR = Path("qiubai_data") # 数据保存目录(脚本所在路径) + # 现代浏览器请求头,避免被反爬 + HEADERS = { + "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 " + "(KHTML, like Gecko) Chrome/129.0.0.0 Safari/537.36", + "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", + "Referer": BASE_URL + } + # 糗事内容解析正则(适配当前糗事百科列表页结构) + CONTENT_PATTERN = re.compile( + r'
选择要导出的任务,系统将生成JSON格式的配置文件。
+加载任务列表...
+从JSON文件导入任务配置。文件应包含任务列表,每个任务包含id和config字段。
+| 任务名称 | +{{ task.config.name }} | +
|---|---|
| 任务描述 | +{{ task.config.get('description', '无') }} | +
| 任务类型 | ++ {% if task.config.type == 'script' %} + Python脚本 + {% elif task.config.type == 'scrapy' %} + Scrapy爬虫 + {% elif task.config.type == 'flask' %} + Flask应用 + {% endif %} + | +
| 任务状态 | ++ {% if task.config.enabled %} + 已启用 + {% else %} + 已禁用 + {% endif %} + | +
| 触发器类型 | ++ {% if task.config.trigger_type == 'cron' %} + Cron表达式 + {% elif task.config.trigger_type == 'interval' %} + 间隔执行 + {% elif task.config.trigger_type == 'date' %} + 指定日期 + {% endif %} + | +
| 触发器配置 | ++ {% if task.config.trigger_type == 'cron' %} + {{ task.config.minute }} {{ task.config.hour }} {{ task.config.day }} {{ task.config.month }} {{ task.config.day_of_week }} + {% elif task.config.trigger_type == 'interval' %} + {% set total_seconds = task.config.get('seconds', 0) + task.config.get('minutes', 0) * 60 + task.config.get('hours', 0) * 3600 %} + {% if total_seconds > 0 %} + 每 + {% if task.config.get('hours', 0) > 0 %}{{ task.config.hours }} 小时{% endif %} + {% if task.config.get('minutes', 0) > 0 %}{{ task.config.minutes }} 分钟{% endif %} + {% if task.config.get('seconds', 0) > 0 %}{{ task.config.seconds }} 秒{% endif %} + 执行一次 + {% else %} + 未配置 + {% endif %} + {% elif task.config.trigger_type == 'date' %} + {{ task.config.run_date }} + {% endif %} + | +
| 任务配置 | +
+ {% if task.config.type == 'script' %}
+ 脚本路径: {{ task.config.script_path }}
+ {% elif task.config.type == 'scrapy' %}
+ 项目路径: {{ task.config.project_path }}
+ 爬虫名称: {{ task.config.spider_name }}
+ {% elif task.config.type == 'flask' %}
+ 应用路径: {{ task.config.app_path }}
+ 函数名称: {{ task.config.function_name }}
+ {% endif %}
+ |
+
加载执行历史...
+{{ task.config.get('description', '无描述') }}
+ +