Skip to main content

基于 asyncio 的高性能异步爬虫框架,支持 MySQL/MongoDB/Redis 等多种数据存储

Project description

AioSpider-tarkin

PyPI version Python Version License

基于 asyncio 的高性能异步爬虫框架

特性安装快速开始文档示例


核心特性

  • 异步高并发:基于 asyncio 的异步架构,支持高并发请求
  • 多数据库支持:内置 MySQL、SQLite、MongoDB、Redis、CSV 支持
  • 强大的 ORM:类似 Django ORM 的数据操作接口
  • 中间件系统:灵活的请求/响应处理中间件
  • 数据去重:支持布隆过滤器、集合等多种去重方式
  • 智能并发控制:多种并发策略(固定、随机、智能)
  • 完整的日志系统:支持控制台、文件、邮件、钉钉等多种输出
  • 命令行工具:丰富的 CLI 工具支持项目管理

安装

基础安装

pip install AioSpider-tarkin

可选依赖

# 安装 Selenium 支持
pip install AioSpider-tarkin[selenium]

# 安装 Playwright 支持
pip install AioSpider-tarkin[playwright]

# 安装额外的数据库支持
pip install AioSpider-tarkin[database]

# 安装所有可选依赖
pip install AioSpider-tarkin[all]

快速开始

1. 创建项目

# 创建新项目
aioSpider create -p myproject

# 进入项目目录
cd myproject

2. 创建爬虫

# spiders/example_spider.py
from AioSpider.http import Request
from AioSpider.spider import Spider

class ExampleSpider(Spider):
    """示例爬虫"""

    name = '示例爬虫'
    source = '示例'

    def start_requests(self):
        """初始化请求"""
        yield Request(
            url='https://api.example.com/data',
            callback=self.parse
        )

    def parse(self, response):
        """解析响应"""
        data = response.json()
        # 处理数据...
        yield {'title': data['title']}

if __name__ == '__main__':
    spider = ExampleSpider()
    spider.start()

3. 配置数据库

# settings.py
from AioSpider.objects import MysqlConnectionData

class DataBaseConfig:
    class Mysql:
        enabled = True
        connect = [
            MysqlConnectionData(
                alias='default',
                host="localhost",
                db="mydb",
                username="root",
                password="password",
                charset="utf8mb4",
            ),
        ]

4. 运行爬虫

# 方式1: 直接运行
python spiders/example_spider.py

# 方式2: 使用命令行运行
aioSpider crawl 示例爬虫

文档

详细文档请查看:

示例

HTTP 请求示例

from AioSpider.http import Request, FormRequest

# GET 请求
yield Request(
    url='https://api.example.com/search',
    params={'keyword': 'python'},
    callback=self.parse
)

# POST 请求
yield FormRequest(
    url='https://api.example.com/login',
    data={'username': 'user', 'password': 'pass'},
    callback=self.after_login
)

数据模型示例

from AioSpider.orm import fields, MySQLModel

class ArticleModel(MySQLModel):
    """文章数据模型"""

    title = fields.CharField(name="标题", max_length=200)
    url = fields.CharField(name="链接", max_length=500, unique=True)
    content = fields.TextField(name="内容")
    publish_date = fields.DateField(name="发布日期")

    class Meta:
        alias = 'default'
        table_name = 't_articles'

响应解析示例

def parse(self, response):
    # JSON 解析
    data = response.json()

    # JSONPath 解析
    items = response.parse_json('data.list', [])

    # XPath 解析
    titles = response.xpath('//h1/text()').getall()

    # CSS 选择器
    links = response.css('a::attr(href)').getall()

适用场景

  • 大规模数据采集
  • 定时数据更新
  • API 数据爬取
  • 数据监控和分析

技术栈

  • 异步框架: asyncio, aiohttp
  • 数据库: MySQL, MongoDB, Redis, SQLite
  • 解析库: lxml, BeautifulSoup4
  • 日志系统: loguru
  • 数据处理: pandas

系统要求

  • Python >= 3.8
  • Windows / Linux / macOS

贡献

欢迎提交 Issue 和 Pull Request!

许可证

本项目采用 MIT 许可证。详见 LICENSE 文件。

作者

Tarkin


如果觉得这个项目对您有帮助,请给一个 ⭐ Star 支持一下!

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

aiospider_tarkin-1.9.7.tar.gz (19.1 MB view details)

Uploaded Source

Built Distribution

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

aiospider_tarkin-1.9.7-py3-none-any.whl (19.2 MB view details)

Uploaded Python 3

File details

Details for the file aiospider_tarkin-1.9.7.tar.gz.

File metadata

  • Download URL: aiospider_tarkin-1.9.7.tar.gz
  • Upload date:
  • Size: 19.1 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.4

File hashes

Hashes for aiospider_tarkin-1.9.7.tar.gz
Algorithm Hash digest
SHA256 64e1a8591decc775a0345dfb5df0b4daf8aa0942017607be94a99b5877343c27
MD5 25b4ac5ad8a44f1005457a521fdf9762
BLAKE2b-256 6f16def14ff036a5c1d74a1a6429fe3bfe579ed5952ac3c128c947094af5acc4

See more details on using hashes here.

File details

Details for the file aiospider_tarkin-1.9.7-py3-none-any.whl.

File metadata

File hashes

Hashes for aiospider_tarkin-1.9.7-py3-none-any.whl
Algorithm Hash digest
SHA256 d19adac345572c5189eb87f33f6ba9b224635246a1b5bbc8f0126fa511d19ba4
MD5 0507c512f9f84cf363563d835a047cbb
BLAKE2b-256 feef0c8a6fde98222c9e4bc849a75dce144c167e254abc13aeedf68e138fa993

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