Skip to content

Commit eeeca05

Browse files
authored
Merge pull request #72 from xboxeer/feature/cli-tool
docs(readme): add CLI usage documentation and update Docker section
2 parents 66ca5e8 + 230bbeb commit eeeca05

File tree

1 file changed

+141
-21
lines changed

1 file changed

+141
-21
lines changed

README.md

Lines changed: 141 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -9,19 +9,138 @@ NScrapy is a distributed spider framework based on .NET Core async programming a
99

1010
---
1111

12-
## 快速开始 / Quick Start
12+
## CLI 工具 / CLI Tool
1313

1414
### 安装 / Installation
1515

16+
```bash
17+
git clone https://github.com/xboxeer/NScrapy.git
18+
cd NScrapy
19+
dotnet build ./NScrapy.Cli/NScrapy.Cli.csproj
20+
dotnet tool install --global --add-path ./NScrapy.Cli/bin/Debug/net10.0/publish
21+
```
22+
23+
### nscrapy new — 创建爬虫项目
24+
25+
创建新的爬虫项目模板:
26+
27+
```bash
28+
nscrapy new <SpiderName> [options]
29+
```
30+
31+
| 参数/选项 | 说明 |
32+
|---|---|
33+
| `<SpiderName>` | 爬虫名称(PascalCase,必填) |
34+
| `-t, --type <TYPE>` | 模板类型:`basic`(默认)或 `distributed` |
35+
| `-o, --output <PATH>` | 输出目录(默认 `.`|
36+
| `--force` | 覆盖已有文件 |
37+
38+
**示例:**
39+
40+
```bash
41+
# 创建基础爬虫
42+
nscrapy new MySpider
43+
44+
# 在指定目录创建
45+
nscrapy new MySpider -o ./spiders
46+
47+
# 创建分布式爬虫
48+
nscrapy new MyDistributedSpider -t distributed
49+
50+
# 覆盖已存在的项目
51+
nscrapy new MySpider --force
52+
```
53+
54+
### nscrapy run — 运行爬虫
55+
56+
运行爬虫程序:
57+
58+
```bash
59+
nscrapy run [spider-name] [options]
60+
```
61+
62+
| 选项 | 说明 |
63+
|---|---|
64+
| `--role <ROLE>` | 运行角色:`single`(默认)、`spider``downloader` |
65+
| `--distributed` | 启用分布式模式 |
66+
| `--redis <HOST:PORT>` | Redis 连接地址 |
67+
| `--redis-password <PWD>` | Redis 密码 |
68+
| `--redis-ssl` | 使用 SSL 连接 Redis |
69+
| `--receiver-queue <NAME>` | 请求队列名称 |
70+
| `--response-queue <NAME>` | 响应队列名称 |
71+
| `--concurrency <N>` | 并发请求数 |
72+
| `--delay <MS>` | 请求间隔(毫秒) |
73+
| `-c, --config <PATH>` | 配置文件路径 |
74+
75+
**示例:**
76+
77+
```bash
78+
# 本地单节点运行
79+
nscrapy run MySpider
80+
81+
# 分布式模式运行
82+
nscrapy run MySpider --role spider --distributed --redis localhost:6379
83+
84+
# 带 Redis 认证和 SSL
85+
nscrapy run MySpider --redis localhost:6380 --redis-password secret --redis-ssl
86+
87+
# 调整并发和请求间隔
88+
nscrapy run MySpider --concurrency 10 --delay 100
89+
90+
# 运行下载器节点
91+
nscrapy run --role downloader --redis localhost:6379
92+
93+
# 使用自定义配置
94+
nscrapy run MySpider -c ./config.json
95+
```
96+
97+
### 配置优先级
98+
99+
配置按以下优先级生效(高 → 低):
100+
101+
1. **CLI 参数** — 命令行直接指定
102+
2. **环境变量**`NSCRAPY_*` 前缀(如 `NSCRAPY_REDIS_HOST`
103+
3. **配置文件** — JSON 配置文件
104+
105+
### 项目结构
106+
107+
`nscrapy new` 生成的结构:
108+
109+
```
110+
MySpider/
111+
├── Spiders/
112+
│ └── MySpiderSpider.cs # 爬虫逻辑
113+
├── Items/
114+
│ └── MySpiderItem.cs # 数据模型
115+
├── Pipelines/
116+
│ └── MySpiderPipeline.cs # 数据处理管道
117+
├── Program.cs # 入口
118+
├── MySpider.csproj
119+
└── appsettings.json # 配置
120+
```
121+
122+
---
123+
124+
## 快速开始 / Quick Start
125+
126+
### 通过 CLI 创建爬虫(推荐)
127+
128+
```bash
129+
nscrapy new MySpider
130+
cd MySpider
131+
nscrapy run MySpider
132+
```
133+
134+
### 通过 NuGet 集成
135+
16136
```bash
17137
dotnet add package NScrapy.Infra
18138
dotnet add package NScrapy.Scheduler
19139
```
20140

21-
### 编写你的第一个爬虫 / Write Your First Spider
141+
使用 Fluent API 编写爬虫:
22142

23143
```csharp
24-
// Local spider
25144
NScrapy.CreateSpider("MySpider")
26145
.StartUrl("https://example.com")
27146
.OnResponse(r => {
@@ -32,13 +151,7 @@ NScrapy.CreateSpider("MySpider")
32151
.Run();
33152
```
34153

35-
启动 NScrapy Shell:
36-
Start the NScrapy Shell:
37-
38-
```csharp
39-
var shell = NScrapy.Shell.NScrapy.GetInstance();
40-
shell.Crawl("JobSpider");
41-
```
154+
> 💡 更多 API 用法参见下方「编程指南」章节。
42155
43156
---
44157

@@ -127,27 +240,34 @@ All settings are controlled via environment variables. The JSON config files ser
127240

128241
```
129242
NScrapy/
130-
├── docker-compose.yml # 三合一 compose 文件 / All-in-one compose for 3 modes
131-
├── Dockerfile.Spider # Spider 多阶段构建 / Multi-stage build for Spider
132-
├── Dockerfile.Downloader # Downloader 多阶段构建 / Multi-stage build for Downloader
133-
├── appsettings.spider.json # Spider 默认配置 / Default Spider config
134-
├── appsettings.downloader.json # Downloader 默认配置 / Default Downloader config
135-
├── NScrapy/ # NScrapy.Shell 库
136-
├── NScrapy.Project/ # Spider 入口 / Spider entry point (Main)
137-
├── NScrapy.DownloaderShell/ # Downloader 工作进程 / Downloader worker
138-
└── ...
243+
├── NScrapy.Cli/ # CLI 工具(nscrapy new / nscrapy run)
244+
├── NScrapy.Core/ # 核心抽象层
245+
├── NScrapy.Engine/ # 爬虫引擎
246+
├── NScrapy.Infra/ # 基础设施(HTTP、解析器等)
247+
├── NScrapy.Scheduler/ # 调度器(InMemory / Redis)
248+
├── NScrapy.Spider/ # Spider 抽象
249+
├── NScrapy.Downloader/ # Downloader 抽象
250+
├── docker-compose.yml # Docker 部署配置(3 种模式)
251+
├── Dockerfile.Spider # Spider 镜像构建
252+
├── Dockerfile.Downloader # Downloader 镜像构建
253+
├── appsettings.spider.json # Spider 默认配置
254+
└── appsettings.downloader.json # Downloader 默认配置
139255
```
140256

141257
### 手动构建镜像 / Building Images Manually
142258

143259
```bash
144260
# 构建 Spider 镜像
145-
# Build Spider image
146261
docker build -f Dockerfile.Spider -t nscrapy-spider .
147262

148263
# 构建 Downloader 镜像
149-
# Build Downloader image
150264
docker build -f Dockerfile.Downloader -t nscrapy-downloader .
265+
266+
# 运行 spider 节点
267+
docker run nscrapy-spider run MySpider --role spider --distributed
268+
269+
# 运行 downloader 节点
270+
docker run nscrapy-downloader run --role downloader
151271
```
152272

153273
### 架构 / Architecture

0 commit comments

Comments
 (0)