-
Notifications
You must be signed in to change notification settings - Fork 2
/
crawler.js
173 lines (147 loc) · 4.63 KB
/
crawler.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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
const Crawler = require('crawler');
const { chromium } = require('playwright');
const ProgressBar = require('progress');
const {
generateProductJson,
generateProductMd,
generateProductPdf,
trim,
} = require('./fileHelper');
function crawProductListPage(config) {
const { title, rateLimit, isProductListCsr } = config;
console.log(`---------->>>>> 开始${rateLimit ? '串行' : '并行'}爬取${title}`);
if (isProductListCsr) {
crawProductListPageCSR(config);
} else {
crawProductListPageSSR(config);
}
}
function crawProductListPageSSR(config) {
const {
productListUrl,
productSelector,
getProductListInfo,
excludeCategory,
} = config;
const crawler = new Crawler();
crawler.direct({
uri: productListUrl,
skipEventRequest: false,
callback: (error, res) => {
if (error) {
console.log(error);
} else {
console.log(`【1】商品列表页:${productListUrl}`);
const productList = { info: [] };
const $ = res.$;
let index = 0;
$(productSelector).map((i, el) => {
const productInfo = getProductListInfo($, el);
if (i === 0) {
productInfo.needFirstCategory = true;
productInfo.needSecondCategory = true;
} else {
const preProd = productList.info[i - index - 1];
productInfo.needFirstCategory =
productInfo.firstCategory === preProd?.firstCategory
? false
: true;
productInfo.needSecondCategory =
productInfo.secondCategory === preProd?.secondCategory
? false
: true && productInfo.secondCategory?.length > 0;
}
if (excludeCategory) {
// 被排除的分类才加入
if (!excludeCategory.includes(trim(productInfo.firstCategory))) {
productList.info.push(productInfo);
} else {
index++;
}
} else {
productList.info.push(productInfo);
}
});
console.log('产品列表页爬取完毕');
generateProductJson(productList.info, config);
crawProductDetail(productList.info, config);
}
},
});
}
async function crawProductListPageCSR(config) {
const { productListUrl, getProductListInfo } = config;
const browser = await chromium.launch();
const context = await browser.newContext();
const page = await context.newPage();
await page.goto(productListUrl);
const productList = await getProductListInfo(page);
// generateProductJson(productList.info, config);
crawProductDetail(productList.info, config);
console.log('产品详情页爬取完毕');
await page.close();
await context.close();
await browser.close();
}
let index = 0;
function crawProductDetail(productList, config) {
const { getProductDetailInfo, rateLimit, maxConnections, title } = config;
const bar = new ProgressBar(
'【2】产品详情页 [:bar] :current :total :percent :etas',
{
width: 50,
total: productList.length,
}
);
const crawlerConfig = {
callback: function (error, res, done) {
if (error) {
console.log(error);
} else {
const $ = res.$;
// const productInfo = productList[index];
const productInfo = productList.find(
(product) => product.link === res.request.uri.href
);
if (productInfo) {
getProductDetailInfo($, productInfo);
} else {
if (rateLimit) {
console.warn(`\n产品列表URL:${productList[index].link}`);
console.warn(`实际跳转URL:${res.request.uri.href}`);
} else {
console.warn(`\n实际跳转URL:${res.request.uri.href}`);
}
}
}
bar.tick();
index++;
done();
},
};
if (rateLimit) {
// 串行爬取
crawlerConfig.rateLimit = rateLimit;
} else {
// 并行爬取
crawlerConfig.maxConnections = maxConnections;
}
const crawler = new Crawler(crawlerConfig);
crawler.queue(productList.map((ele) => ele.link));
crawler.on('drain', () => {
console.log('产品详情页爬取完毕');
generateProductJson(productList, config);
generateProductMd(productList, config);
// generateProductPdf(config);
console.log(`<<<<<---------- ${title}爬取完成`);
});
}
function analyzeProduct(productList) {
const firstCategories = productList.filter(
(product) => products.needFirstCategory
);
const secondCategories = productList.filter(
(product) => products.needSecondCategory
);
}
module.exports = { crawProductListPage, crawProductDetail, trim };