-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurl_all.js
69 lines (60 loc) · 2.11 KB
/
curl_all.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
const cheerio = require('cheerio')
const async = require('async')
const fetch = require('node-fetch')
let index = 1
// 抓取网页内容
function fetchUrl(target, callback) {
const { href, name } = target
const idx = index++
console.log(`第 ${idx} 个开始抓取 ...`);
fetch(href, {
"headers": {
"accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"accept-language": "zh-CN,zh;q=0.9,en;q=0.8",
"cache-control": "no-cache",
"pragma": "no-cache",
"sec-ch-ua": "\"Chromium\";v=\"110\", \"Not A(Brand\";v=\"24\", \"Google Chrome\";v=\"110\"",
"sec-ch-ua-mobile": "?0",
"sec-ch-ua-platform": "\"macOS\"",
"sec-fetch-dest": "document",
"sec-fetch-mode": "navigate",
"sec-fetch-site": "same-origin",
"sec-fetch-user": "?1",
"upgrade-insecure-requests": "1"
},
"referrerPolicy": "strict-origin-when-cross-origin",
"body": null,
"method": "GET"
}).then(res => res.text())
.then(html => {
const $ = cheerio.load(html)
const ans = []
$('table tbody').eq(0).children('tr').each(function (index, item) {
const $item = $(item)
const tds = $item.children('td')
const className = $(tds[0]).text().trim();
// workflow 中显示内容只能显示一行,\n 后的内容无法显示
const detail = $(tds[1]).text().trim().replace(/\n/g, ' ')
ans.push({
className, // tailwind 中的类名
detail, // 该类型表示的实际样式
})
})
console.log(`第 ${idx} 个抓取结束`);
callback(null, {
name,
list: ans
});
})
}
// 目前筛选后共 52 条数据
const targets = require('./menu.json');
// 并发量控制为 5
// 对每个元素执行第三个回调
// 全部执行完后执行第四个回调
async.mapLimit(targets, 5, function (item, callback) {
fetchUrl(item, callback);
}, function (err, result) {
console.log('---抓取成功---')
require('fs').writeFileSync('class-list.json', JSON.stringify(result))
});