Skip to main content

爬虫脚手架框架 - 快速创建和运行爬虫任务

Project description

spiderWei_TecDo 爬虫框架

快速创建和运行爬虫任务的脚手架框架。

功能作用说明

  • 命令行一键生成爬虫脚手架,统一项目结构与入口
  • 统一配置管理(Apollo/本地直配),支持配置覆盖与环境切换
  • 内置数据库、Kafka、OBS/OSS、代理、下载队列等能力
  • 支持并发执行与循环执行(按间隔/每日时间点)

安装

运行环境要求:Python >= 3.8

# 开发模式安装(推荐)
cd spiderWei
pip install -e .

# 或直接安装
pip install spiderWei-TecDo

快速开始

1. 创建爬虫

# 在当前目录创建爬虫
spiderWei_TecDo start my_spider

# 在指定目录创建
spiderWei_TecDo start my_spider -o ./spiders

2. 查看配置说明

spiderWei_TecDo list

3. 编辑生成的爬虫文件

打开生成的 my_spider.py,修改以下内容:

  1. Apollo 配置 - 根据实际环境修改

    APOLLOID='Your-Apollo-ID',
    APOLLO_URL_TEST='http://your-test-apollo.com',
    APOLLO_URL_PROD='http://your-prod-apollo.com',
    
  2. 数据推送配置 - 二选一

    DB_table_name='your_table_name',  # 直接写表
    # 或
    DB_topic_name='your_topic_name',  # 写 Kafka
    
  3. 实现 getexeList() 函数 - 构造任务列表

    def getexeList():
        return [{"id": 1}, {"id": 2}, {"id": 3}]
    
  4. 实现 main(arg, app) 函数 - 编写采集逻辑

    def main(arg, app):
        # 发起请求
        result = app.crawl.crawl(f"https://api.example.com/{arg['id']}")
        data = json.loads(result['html'])
        
        # 推送数据
        app.push_queue.put({"id": arg['id'], "data": data})
    

4. 运行爬虫

python my_spider.py

5. 配置循环执行(可选)

SpiderSettings 中开启循环执行:

settings = SpiderSettings(
    loop_enable=1,
    loop_mode="interval",           # interval 或 daily_time
    loop_interval_seconds=1800,      # interval 模式:每次结束到下次开始的间隔
    # loop_daily_times=["00:00", "12:00"],  # daily_time 模式
)

开启后,run_spider 会按配置持续循环执行任务。

配置项说明

配置项 说明 默认值
Apollo配置
apollo_enable 是否启用 Apollo 1
APOLLOID Apollo 应用ID 'Creatives-spider'
APOLLO_URL_TEST 测试环境 Apollo 地址 'http://dev-apollo.tec-develop.com'
APOLLO_URL_PROD 生产环境 Apollo 地址 'http://hwsgprod.in.apolloconfig...:8080'
环境配置
isTest 从环境变量 ISTEST 获取 默认 1 (测试环境)
Threadnum 并发线程数 1
代理配置
openproXies 是否启用代理 0 (不启用)
IPlist Apollo 中代理IP列表 key 名 'IPlist'
proxy_list 直接指定代理列表 None
数据库配置
readDb 是否连接数据库 0 (不连接)
DB_table_name 数仓表名 ''
DB_topic_name Kafka Topic ''
PRIMARY_KEY_NAME 主键名 ''
holoHost 数据库 Host ''
holoPort 数据库 Port ''
holoDBname 数据库名称 ''
holoUser 数据库用户 ''
holoPassword 数据库密码 ''
Kafka配置
needKafka 是否需要Kafka 0 (不需要)
kafkaAddress Kafka 地址列表 None
download_topic_name 下载器Topic ''
OBS/OSS配置
needOBS 是否需要对象存储 0 (不需要)
isHwOBS OBS类型 0 (阿里云)
HWOBS_AK 华为云OBS AK ''
HWOBS_SK 华为云OBS SK ''
HWOBS_Endpoint 华为云OBS Endpoint ''
HWOBS_BucketName 华为云OBS BucketName ''
HWOBS_Link 华为云OBS 访问链接前缀 ''
AliOSS_AK 阿里云OSS AK ''
AliOSS_SK 阿里云OSS SK ''
AliOSS_BucketName 阿里云OSS BucketName ''
AliOSS_Endpoint 阿里云OSS Endpoint ''
AliOSS_Link 阿里云OSS 访问链接前缀 ''
调试配置
is_print_item 是否打印推送数据 0 (不打印)
循环执行配置
loop_enable 是否循环执行 0 (不启用)
loop_mode 循环模式 "interval"
loop_interval_seconds 间隔秒数 3600
loop_daily_times 每日时间点列表 None

配置覆盖说明

配置优先级(高 → 低):

  1. SpiderSettings 中直接指定的配置
  2. Apollo 配置(当 apollo_enable=1
  3. 代码默认值

覆盖规则示例:

  • isTest 默认从环境变量 ISTEST 读取,但可通过 SpiderSettings.isTest 覆盖
  • kafkaAddressproxy_listholoHostHWOBS_AK/AliOSS_AK 等存在直配时优先使用直配
  • apollo_enable=0 时不拉取 Apollo 配置,仅使用直配与默认值

环境变量

# Windows
set ISTEST=0    # 0=生产环境, 1=测试环境

# Linux/Mac
export ISTEST=0

app 对象属性

main(arg, app) 函数中,app 对象提供以下属性:

  • app.push_queue - 数据推送队列
  • app.download_queue - 下载器队列
  • app.holo_client - 数据库客户端
  • app.OBSHelper - OBS/OSS 客户端
  • app.videoOp - 视频操作工具
  • app.fileOp - 文件操作工具
  • app.crawl - 爬虫请求工具
  • app.configurations - Apollo 配置

依赖

  • requests>=2.25.0
  • psycopg>=3.0.0
  • kafka-python>=2.0.0
  • yt-dlp>=2023.0.0
  • esdk-obs-python>=3.0.0
  • oss2>=2.15.0

开发依赖(可选)

pip install "spiderWei-TecDo[dev]"
  • pytest>=7.0.0
  • black>=22.0.0

项目地址

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

spiderwei_tecdo-1.3.0.tar.gz (20.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

spiderwei_tecdo-1.3.0-py3-none-any.whl (21.5 kB view details)

Uploaded Python 3

File details

Details for the file spiderwei_tecdo-1.3.0.tar.gz.

File metadata

  • Download URL: spiderwei_tecdo-1.3.0.tar.gz
  • Upload date:
  • Size: 20.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.9

File hashes

Hashes for spiderwei_tecdo-1.3.0.tar.gz
Algorithm Hash digest
SHA256 1bd3b671ab7dfa195482b1d15097ef99ff5bfff8aafe1923e5b4b90bf6f77a1c
MD5 e7c890a3710b55ee7a816f75632e8496
BLAKE2b-256 a9525cfb833455a86ef53ac643c8ebce9e18c68ccb2fae83ee8cfe4084d3da78

See more details on using hashes here.

File details

Details for the file spiderwei_tecdo-1.3.0-py3-none-any.whl.

File metadata

File hashes

Hashes for spiderwei_tecdo-1.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 0cc68dd6d95ee6427e0283e85fc6131d4789505e931aebd32d1d201e75b2359a
MD5 453ed9924797d4d6519a9cc5df096943
BLAKE2b-256 1fd6876d8ead0b99f633ef4369be7953298256e6d3b0aee4ddb69735664d9cba

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page