OpenClaw (小龙虾) 安装部署指南
OpenClaw 是一款开源的分布式爬虫框架,支持高并发、断点续爬等功能。
环境要求
- Python 3.9+
- Redis 5.0+
- MongoDB 4.4+ 或 MySQL 8.0+
- Linux/Unix 系统(推荐)
快速安装
1. 安装依赖
# 安装系统依赖 sudo apt update sudo apt install -y python3-pip python3-venv redis-server安装 MongoDB
wget -qO - https://www.mongodb.org/static/pgp/server-6.0.asc | sudo apt-key add - echo “deb [ arch=amd64,arm64 ] https://repo.mongodb.org/apt/ubuntu jammy/mongodb-org/6.0 multiverse” | sudo tee /etc/apt/sources.list.d/mongodb-org-6.0.list sudo apt update sudo apt install -y mongodb-org
2. 克隆项目
git clone https://github.com/openclaw/openclaw.git
cd openclaw
git checkout stable # 切换到稳定分支
3. 创建虚拟环境
python3 -m venv venv source venv/bin/activate安装 Python 依赖
pip install -r requirements.txt
配置说明
基础配置
# config/settings.yaml database: type: mongodb host: localhost port: 27017 name: openclaw_dbredis: host: localhost port: 6379 db: 0
crawler: max_workers: 10 request_timeout: 30 retry_times: 3 user_agent: “OpenClaw/1.0”
启动服务
# 启动 Redis redis-server /etc/redis/redis.conf启动 MongoDB
sudo systemctl start mongod
启动 OpenClaw
python main.py —config config/settings.yaml
创建爬虫任务
示例爬虫
# spiders/example.py
from openclaw import Spider, Request
class ExampleSpider(Spider):
name = “example”
start_urls = [“https://example.com”]
def parse(self, response):
title = response.css("h1::text").get()
yield {"title": title}
# 跟随链接
for href in response.css("a::attr(href)"):
yield Request(href.get(), callback=self.parse)</code></pre>
运行爬虫
# 运行单个爬虫
python main.py crawl example
并发运行多个爬虫
python main.py crawl spider1 spider2 spider3
定时任务
python main.py schedule —cron “0 */2 * * *” example
高级功能
代理配置
# config/proxies.yaml
proxies:
- http://user:pass@proxy1.com:8080
- http://user:pass@proxy2.com:8080
proxy_rotation: true
proxy_check: true
数据导出
# 导出为 JSON
python main.py export --format json --output data.json
导出为 CSV
python main.py export —format csv —output data.csv
直接写入数据库
python main.py export —database mongodb
常见问题
Q: 爬虫被反爬?
A: 配置代理池,降低爬取频率,设置合理的 User-Agent。
Q: 内存占用过高?
A: 调整 max_workers 参数,启用数据分批处理。