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

可选依赖

AioSpider-tarkin 的核心功能无需 C 编译环境。以下是可选依赖的安装方式:

# 安装 MariaDB 支持(需要 C 编译环境)
pip install AioSpider-tarkin[mariadb]

# 安装 Oracle 支持(需要 C 编译环境和 Oracle Instant Client)
pip install AioSpider-tarkin[oracle]

# 安装 PostgreSQL 支持(需要 C 编译环境)
pip install AioSpider-tarkin[postgresql]

# 安装 SQL Server 支持(需要 C 编译环境和 ODBC 驱动)
pip install AioSpider-tarkin[sqlserver]

# 安装浏览器自动化支持(需要浏览器驱动)
pip install AioSpider-tarkin[browser]

# 安装高性能字符编码检测(需要 C 编译环境)
pip install AioSpider-tarkin[cchardet]

# 安装所有可选依赖(需要 C 编译环境)
pip install AioSpider-tarkin[full]

注意:如果您的环境没有 C 编译器(如 Windows 上的 Microsoft Visual C++),建议只安装基础版本,框架会使用纯 Python 实现的替代包。

快速开始

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.9.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.9-py3-none-any.whl (19.2 MB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: aiospider_tarkin-1.9.9.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.9.tar.gz
Algorithm Hash digest
SHA256 e6d57984b33f4b66c5d35698657ffd7954e31fd0a2283ad09774db8429c8415f
MD5 d0e98e350711cb19a157fa100be8279f
BLAKE2b-256 d342f1983de1750df40f9a5ec691f383ade3f3f0b344ce33e94333059842975b

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for aiospider_tarkin-1.9.9-py3-none-any.whl
Algorithm Hash digest
SHA256 df48840a5f284c46da1304b3b3779ca97dc921689f4704884b921338e83b6227
MD5 64dbc60f971bf99fa9b6f1c0145e3f22
BLAKE2b-256 beef3aa19f13d127c9055aa856c22c73ccb3f4c6a66e0427910624f424cc4bb9

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