Skip to content

Commit bda67b7

Browse files
committed
Update the Chinese document
1 parent e6ae399 commit bda67b7

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

document/cn.md

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,26 +20,36 @@ npm install x-crawl
2020

2121
## 示例
2222

23-
获取 https://docs.github.com/zh/get-started 的标题为例:
23+
获取 bilibili 国漫主页的推荐轮播图片为例:
2424

2525
```js
26-
// 导入模块 ES/CJS
26+
// 1.导入模块 ES/CJS
2727
import XCrawl from 'x-crawl'
2828
29-
// 创建一个爬虫实例
30-
const docsXCrawl = new XCrawl({
31-
baseUrl: 'https://docs.github.com',
32-
timeout: 10000,
33-
intervalTime: { max: 2000, min: 1000 }
29+
// 2.创建一个爬虫实例
30+
const myXCrawl = new XCrawl({
31+
baseUrl: 'https://www.bilibili.com',
32+
timeout: 10000, // 超时时间
33+
intervalTime: { max: 6000, min: 2000 } // 控制请求频率
3434
})
3535
36-
// 调用 fetchHTML API 爬取
37-
docsXCrawl.fetchHTML('/zh/get-started').then((res) => {
38-
const { jsdom } = res.data
39-
console.log(jsdom.window.document.querySelector('title')?.textContent)
36+
// 3.调用 fetchHTML API 爬取 HTML
37+
myXCrawl.fetchHTML('/guochuang/').then((res) => {
38+
const { jsdom } = res.data // 默认使用了 JSDOM 库解析 HTML
39+
40+
// 3.1.获取轮播图片的 src
41+
const imgSrc: string[] = []
42+
const recomEls = jsdom.window.document.querySelectorAll('.chief-recom-item')
43+
recomEls.forEach((item) => imgSrc.push(item.querySelector('img').src))
44+
45+
// 3.2.调用 fetchFile API 爬取图片
46+
const requestConifg = imgSrc.map((src) => ({ url: `https:${src}` }))
47+
myXCrawl.fetchFile({ requestConifg, fileConfig: { storeDir: './upload' } })
4048
})
4149
```
4250

51+
**注意:** 请勿随意爬取,这里只是为了演示爬取过程,并将请求频率控制在 6000ms 到 2000ms 内。
52+
4353
## 核心概念
4454

4555
### XCrawl

0 commit comments

Comments
 (0)