基于 asyncio 的高性能异步爬虫框架,支持 MySQL/MongoDB/Redis 等多种数据存储
Project description
AioSpider-tarkin
核心特性
- ✅ 异步高并发:基于 asyncio 的异步架构,支持高并发请求
- ✅ 多数据库支持:内置 MySQL、SQLite、MongoDB、Redis、CSV 支持
- ✅ 强大的 ORM:类似 Django ORM 的数据操作接口
- ✅ 中间件系统:灵活的请求/响应处理中间件
- ✅ 数据去重:支持布隆过滤器、集合等多种去重方式
- ✅ 智能并发控制:多种并发策略(固定、随机、智能)
- ✅ 完整的日志系统:支持控制台、文件、邮件、钉钉等多种输出
- ✅ 命令行工具:丰富的 CLI 工具支持项目管理
安装
基础安装
pip install AioSpider-tarkin
可选依赖
AioSpider-tarkin 的核心功能无需 C 编译环境。以下是可选依赖的安装方式:
# 安装高性能字符编码检测(需要 C 编译环境)
pip install AioSpider-tarkin[cchardet]
# 安装 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]
# 安装所有可选依赖(需要 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 示例爬虫
文档
详细文档请查看:
- 完整使用指南
- API 文档(即将推出)
示例
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file aiospider_tarkin-1.9.12.tar.gz.
File metadata
- Download URL: aiospider_tarkin-1.9.12.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
737016da5721ae90a71517752231b3289da08f79148d4c4a2d7a8768b82a3ed9
|
|
| MD5 |
6ed9d0f7ff68d36d96d894428e3272a7
|
|
| BLAKE2b-256 |
7940ea367725f42fc12765c2de49d76b140036a3f1737212f098b53499f4fdbe
|
File details
Details for the file aiospider_tarkin-1.9.12-py3-none-any.whl.
File metadata
- Download URL: aiospider_tarkin-1.9.12-py3-none-any.whl
- Upload date:
- Size: 19.2 MB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.10.4
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7dc2cb1f7218b74df56ebf28858404b2bd831a2c09a5b00a57094badef9e0fbd
|
|
| MD5 |
14be88307aeda0fc49cd58bb854a3304
|
|
| BLAKE2b-256 |
d4dbb5f6e93a8ed0b386208da567f02df029ba498c746afbb28d1381ff9999f5
|