Skip to content

Commit 1808710

Browse files
committed
docs: update
1 parent b367332 commit 1808710

File tree

5 files changed

+348
-63
lines changed

5 files changed

+348
-63
lines changed

README.md

+132-7
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ The following can be done:
2929
- [Install](#Install)
3030
- [Example](#Example)
3131
- [Core concepts](#Core-concepts)
32+
* [Create application](#Create-application)
33+
+ [An example of a crawler application](#An-example-of-a-crawler-application)
34+
+ [Choose crawling mode](#Choose-crawling-mode)
35+
+ [Set interval time](#Set-interval-time)
36+
+ [Multiple crawler application instances](#Multiple-crawler-application-instances)
37+
* [Crawl page](#Crawl-page)
38+
* [Crawl interface](#Crawl-interface)
39+
* [Crawl files](#Crawl-files)
40+
- [API](#API)
3241
* [x-crawl](#x-crawl-2)
3342
+ [Type](#Type-1)
3443
+ [Example](#Example-1)
@@ -128,6 +137,125 @@ running result:
128137

129138
## Core concepts
130139

140+
### Create application
141+
142+
#### An example of a crawler application
143+
144+
Create a new **application instance** via [xCrawl()](#xCrawl):
145+
146+
```js
147+
import xCrawl from 'x-crawl'
148+
149+
const myXCrawl = xCrawl({
150+
// options
151+
})
152+
```
153+
154+
Related **options** can refer to [XCrawlBaseConfig](#XCrawlBaseConfig) .
155+
156+
#### Choose crawling mode
157+
158+
A crawler application instance has two crawling modes: asynchronous/synchronous, and each crawler instance can only choose one of them.
159+
160+
```js
161+
import xCrawl from 'x-crawl'
162+
163+
const myXCrawl = xCrawl({
164+
mode: 'async'
165+
})
166+
```
167+
168+
The mode option defaults to async .
169+
170+
- async: asynchronous request, in batch requests, the next request is made without waiting for the current request to complete
171+
- sync: synchronous request, in batch requests, you need to wait for this request to complete before making the next request
172+
173+
If there is an interval time set, it is necessary to wait for the interval time to end before sending the request.
174+
175+
#### Set interval time
176+
177+
Setting the interval time can prevent too much concurrency and avoid too much pressure on the server.
178+
179+
```js
180+
import xCrawl from 'x-crawl'
181+
182+
const myXCrawl = xCrawl({
183+
intervalTime: { max: 3000, min: 1000 }
184+
})
185+
```
186+
187+
The intervalTime option defaults to undefined . If there is a setting value, it will wait for a period of time before requesting, which can prevent too much concurrency and avoid too much pressure on the server.
188+
189+
- number: The time that must wait before each request is fixed
190+
- Object: Randomly select a value from max and min, which is more anthropomorphic
191+
192+
The first request is not to trigger the interval.
193+
194+
#### Multiple crawler application instances
195+
196+
```js
197+
import xCrawl from 'x-crawl'
198+
199+
const myXCrawl1 = xCrawl({
200+
// options
201+
})
202+
203+
const myXCrawl2 = xCrawl({
204+
// options
205+
})
206+
```
207+
208+
### Crawl page
209+
210+
Fetch a page via [fetchPage()](#fetchPage)
211+
212+
```js
213+
myXCrawl.fetchPage('https://xxx.com').then(res => {
214+
const { jsdom, page } = res.data
215+
})
216+
```
217+
218+
### Crawl interface
219+
220+
Crawl interface data through [fetchData()](#fetchData)
221+
222+
```js
223+
const requestConfig = [
224+
{ url: 'https://xxx.com/xxxx' },
225+
{ url: 'https://xxx.com/xxxx' },
226+
{ url: 'https://xxx.com/xxxx' }
227+
]
228+
229+
myXCrawl.fetchData({ requestConfig }).then(res => {
230+
// deal with
231+
})
232+
```
233+
234+
### Crawl files
235+
236+
Fetch file data via [fetchFile()](#fetchFile)
237+
238+
```js
239+
import path from 'node:path'
240+
241+
const requestConfig = [
242+
{ url: 'https://xxx.com/xxxx' },
243+
{ url: 'https://xxx.com/xxxx' },
244+
{ url: 'https://xxx.com/xxxx' }
245+
]
246+
247+
myXCrawl. fetchFile({
248+
requestConfig,
249+
fileConfig: {
250+
storeDir: path.resolve(__dirname, './upload') // storage folder
251+
}
252+
}).then(fileInfos => {
253+
console. log(fileInfos)
254+
})
255+
```
256+
257+
## API
258+
131259
### x-crawl
132260

133261
Create a crawler instance via call xCrawl. The request queue is maintained by the instance method itself, not by the instance itself.
@@ -226,15 +354,12 @@ function fetchData: <T = any>(
226354

227355
```js
228356
const requestConfig = [
229-
{ url: '/xxxx', method: 'GET' },
230-
{ url: '/xxxx', method: 'GET' },
231-
{ url: '/xxxx', method: 'GET' }
357+
{ url: '/xxxx' },
358+
{ url: '/xxxx' },
359+
{ url: '/xxxx' }
232360
]
233361
234-
myXCrawl.fetchData({
235-
requestConfig,
236-
intervalTime: { max: 5000, min: 1000 }
237-
}).then(res => {
362+
myXCrawl.fetchData({ requestConfig }).then(res => {
238363
console.log(res)
239364
})
240365
```

docs/cn.md

+82-47
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ fetchPage API 内部使用 [puppeteer](https://github.com/puppeteer/puppeteer)
3131
- [安装](#安装)
3232
- [示例](#示例)
3333
- [核心概念](#核心概念)
34-
* [创建第一个爬虫实例](#创建第一个爬虫实例)
34+
* [创建应用](#创建应用)
35+
+ [一个爬虫应用实例](#一个爬虫应用实例)
36+
+ [选择爬取模式](#选择爬取模式)
37+
+ [设置间隔时间](#设置间隔时间)
38+
+ [多个爬虫应用实例](#多个爬虫应用实例)
3539
* [爬取页面](#爬取页面)
3640
* [爬取接口](#爬取接口)
3741
* [爬取文件](#爬取文件)
@@ -127,7 +131,11 @@ myXCrawl.startPolling({ d: 1 }, () => {
127131
128132
## 核心概念
129133
130-
### 创建一个爬虫应用实例
134+
### 创建应用
135+
136+
#### 一个爬虫应用实例
137+
138+
通过 [xCrawl()](#xCrawl) 创建一个新的 **应用实例:**
131139
132140
```js
133141
import xCrawl from 'x-crawl'
@@ -137,48 +145,98 @@ const myXCrawl = xCrawl({
137145
})
138146
```
139147
140-
有关选项内容可参考 [XCrawlBaseConfig](#XCrawlBaseConfig) 。
148+
相关的 **选项** 可参考 [XCrawlBaseConfig](#XCrawlBaseConfig) 。
149+
150+
#### 选择爬取模式
151+
152+
一个爬虫应用实例有两种爬取模式: 异步/同步,每个爬虫实例只能选择其中一种。
153+
154+
```js
155+
import xCrawl from 'x-crawl'
156+
157+
const myXCrawl = xCrawl({
158+
mode: 'async'
159+
})
160+
```
161+
162+
mode 选项默认为 async 。
163+
164+
- async: 异步请求,在批量请求时,无需等当前请求完成,就进行下次请求
165+
- sync: 同步请求,在批量请求时,需要等这次请求完成,才会进行下次请求
166+
167+
若有设置间隔时间,则都需要等间隔时间结束才能发送请求。
168+
169+
#### 设置间隔时间
170+
171+
设置间隔时间可以防止并发量太大,避免给服务器造成太大的压力。
172+
173+
```js
174+
import xCrawl from 'x-crawl'
175+
176+
const myXCrawl = xCrawl({
177+
intervalTime: { max: 3000, min: 1000 }
178+
})
179+
```
180+
181+
intervalTime 选项默认为 undefined 。若有设置值,则会在请求前等待一段时间,可以防止并发量太大,避免给服务器造成太大的压力。
182+
183+
- number: 固定每次请求前必须等待的时间
184+
- Object: 在 max 和 min 中随机取一个值,更加拟人化
185+
186+
第一次请求是不会触发间隔时间。
187+
188+
189+
#### 多个爬虫应用实例
190+
191+
```js
192+
import xCrawl from 'x-crawl'
193+
194+
const myXCrawl1 = xCrawl({
195+
// 选项
196+
})
197+
198+
const myXCrawl2 = xCrawl({
199+
// 选项
200+
})
201+
```
141202
142203
### 爬取页面
143204
144-
可以通过 [fetchPage()](#fetchPage) 爬取接口数据
205+
通过 [fetchPage()](#fetchPage) 爬取一个页面
145206
146207
```js
147208
myXCrawl.fetchPage('https://xxx.com').then(res => {
148-
const { jsdom, page } = res
209+
const { jsdom, page } = res.data
149210
})
150211
```
151212
152213
### 爬取接口
153214
154-
可以通过 [fetchData()](#fetchData) 爬取接口数据
215+
通过 [fetchData()](#fetchData) 爬取接口数据
155216
156217
```js
157218
const requestConfig = [
158-
{ url: '/xxxx', method: 'GET' },
159-
{ url: '/xxxx', method: 'GET' },
160-
{ url: '/xxxx', method: 'GET' }
219+
{ url: 'https://xxx.com/xxxx' },
220+
{ url: 'https://xxx.com/xxxx' },
221+
{ url: 'https://xxx.com/xxxx' }
161222
]
162223
163-
myXCrawl.fetchData({
164-
requestConfig,
165-
intervalTime: { max: 5000, min: 1000 }
166-
}).then(res => {
167-
console.log(res)
224+
myXCrawl.fetchData({ requestConfig }).then(res => {
225+
// 处理
168226
})
169227
```
170228
171229
### 爬取文件
172230
173-
可以通过 [fetchFile()](#fetchFile) 爬取文件数据
231+
通过 [fetchFile()](#fetchFile) 爬取文件数据
174232
175233
```js
176234
import path from 'node:path'
177235
178236
const requestConfig = [
179-
{ url: '/xxxx' },
180-
{ url: '/xxxx' },
181-
{ url: '/xxxx' }
237+
{ url: 'https://xxx.com/xxxx' },
238+
{ url: 'https://xxx.com/xxxx' },
239+
{ url: 'https://xxx.com/xxxx' }
182240
]
183241
184242
myXCrawl.fetchFile({
@@ -195,7 +253,7 @@ myXCrawl.fetchFile({
195253
196254
### xCrawl
197255
198-
通过调用 xCrawl 创建一个爬虫实例。请求队列是由实例方法内部自己维护,并非由实例自己维护。
256+
通过调用 xCrawl 创建一个爬虫实例。请求是由实例方法内部自己维护,并非由实例自己维护。
199257
200258
#### 类型
201259
@@ -219,31 +277,11 @@ const myXCrawl = xCrawl({
219277
})
220278
```
221279
222-
传入 **baseConfig** 是为了让 **fetchPage/fetchData/fetchFile** 默认使用这些值。
223-
224280
**注意:** 为避免后续示例需要重复创建实例,这里的 **myXCrawl** 将是 **fetchPage/fetchData/fetchFile** 示例中的爬虫实例。
225281
226-
#### 模式
227-
228-
mode 选项默认为 async 。
229-
230-
- async: 在批量请求时,无需等当前请求完成,就进行下次请求
231-
- sync: 在批量请求时,需要等这次请求完成,才会进行下次请求
282+
### fetchPage
232283
233-
若有设置间隔时间,则都需要等间隔时间结束才能发送请求。
234-
235-
#### 间隔时间
236-
237-
intervalTime 选项默认为 undefined 。若有设置值,则会在请求前等待一段时间,可以防止并发量太大,避免给服务器造成太大的压力。
238-
239-
- number: 固定每次请求前必须等待的时间
240-
- Object: 在 max 和 min 中随机取一个值,更加拟人化
241-
242-
第一次请求是不会触发间隔时间。
243-
244-
### fetchPage
245-
246-
fetchPage 是 [myXCrawl](https://github.com/coder-hxl/x-crawl/blob/main/document/cn.md#%E7%A4%BA%E4%BE%8B-1) 实例的方法,通常用于爬取页面。
284+
fetchPage 是 [myXCrawl](#示例-2) 实例的方法,通常用于爬取页面。
247285
248286
#### 类型
249287
@@ -272,7 +310,7 @@ myXCrawl.fetchPage('/xxx').then((res) => {
272310
273311
### fetchData
274312
275-
fetch 是 [myXCrawl](#示例-1) 实例的方法,通常用于爬取 API ,可获取 JSON 数据等等。
313+
fetch 是 [myXCrawl](#示例-2) 实例的方法,通常用于爬取 API ,可获取 JSON 数据等等。
276314
277315
#### 类型
278316
@@ -296,17 +334,14 @@ const requestConfig = [
296334
{ url: '/xxxx', method: 'GET' }
297335
]
298336
299-
myXCrawl.fetchData({
300-
requestConfig,
301-
intervalTime: { max: 5000, min: 1000 }
302-
}).then(res => {
337+
myXCrawl.fetchData({ requestConfig }).then(res => {
303338
console.log(res)
304339
})
305340
```
306341
307342
### fetchFile
308343
309-
fetchFile 是 [myXCrawl](#示例-1) 实例的方法,通常用于爬取文件,可获取图片、pdf 文件等等。
344+
fetchFile 是 [myXCrawl](#示例-2) 实例的方法,通常用于爬取文件,可获取图片、pdf 文件等等。
310345
311346
#### 类型
312347

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
{
22
"private": true,
33
"name": "x-crawl",
4-
"version": "2.3.0",
4+
"version": "2.3.1",
55
"author": "coderHXL",
66
"description": "XCrawl is a Nodejs multifunctional crawler library.",
77
"license": "MIT",

0 commit comments

Comments
 (0)