Skip to content

Commit 6299d4b

Browse files
committed
docs: minor amendment
1 parent 89751cd commit 6299d4b

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ XCrawl is a Nodejs multifunctional crawler library.
1212
- Support Promise/Callback way to get the result
1313
- Polling function
1414
- Anthropomorphic request interval
15-
- Written in TypeScript
15+
- Written in TypeScript, provides generics
1616

1717
# Table of Contents
1818

docs/cn.md

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ XCrawl 是 Nodejs 多功能爬虫库。
1212
- 支持 Promise/Callback 方式获取结果
1313
- 轮询功能
1414
- 拟人化的请求间隔时间
15-
- 使用 TypeScript 编写
15+
- 使用 TypeScript 编写,提供泛型
1616

1717
# 目录
1818

@@ -75,24 +75,32 @@ const myXCrawl = new XCrawl({
7575
intervalTime: { max: 3000, min: 2000 } // 控制请求频率
7676
})
7777
78-
// 3.调用 fetchPolling API 开始轮询功能,每隔一天会调用回调函数
78+
// 3.设置爬取任务
79+
// 调用 fetchPolling API 开始轮询功能,每隔一天会调用回调函数
7980
myXCrawl.fetchPolling({ d: 1 }, () => {
80-
// 3.1.调用 fetchHTML API 爬取 HTML
81+
// 调用 fetchHTML API 爬取 HTML
8182
myXCrawl.fetchHTML('https://www.bilibili.com/guochuang/').then((res) => {
82-
const { jsdom } = res.data // 默认使用了 JSDOM 库解析 HTML
83-
84-
// 3.2.获取轮播图片的 src
85-
const imgSrc = []
86-
const recomEls = jsdom.window.document.querySelectorAll('.chief-recom-item')
87-
recomEls.forEach((item) => imgSrc.push(item.querySelector('img').src))
88-
89-
// 3.3.调用 fetchFile API 爬取图片
90-
const requestConifg = imgSrc.map((src) => ({ url: `https:${src}` }))
83+
const { jsdom } = res.data // 默认使用了 JSDOM 库解析 HTML
84+
85+
// 获取轮播图片元素
86+
const imgEls = jsdom.window.document.querySelectorAll('.chief-recom-item img')
87+
88+
// 设置请求配置
89+
const requestConifg = []
90+
imgEls.forEach((item) => requestConifg.push({ url: `https:${item.src}` }))
91+
92+
// 调用 fetchFile API 爬取图片
9193
myXCrawl.fetchFile({ requestConifg, fileConfig: { storeDir: './upload' } })
9294
})
9395
})
9496
```
9597
98+
运行效果:
99+
100+
![](https://raw.githubusercontent.com/coder-hxl/x-crawl/main/assets/crawler.png)
101+
102+
![](https://raw.githubusercontent.com/coder-hxl/x-crawl/main/assets/crawler-result.png)
103+
96104
**注意:** 请勿随意爬取,这里只是为了演示如何使用 XCrawl ,并将请求频率控制在 3000ms 到 2000ms 内。
97105
98106
## 核心概念

0 commit comments

Comments
 (0)