Я создаю сканер с помощью Node.js.
На целевой веб-странице имеется более 10 категорий.
Я могу получить их с помощью своего сканера.
И я делаю запросы для каждой категории.(10+ запросов)
Затем на каждой странице категории есть более 100 элементов.
И я делаю запросы для каждого элемента.(100+ запросов)
Так что мне нужно 10+ * 100+ запросов!
Мой код здесь.
const axios = require("axios")
const cheerio = require("cheerio");
async function request(url) {
return await axios.get(url);
}
function main() {
request(url).then(html => {
const $ = cheerio.load(html.data);
const categoryArray = $('table.table tbody').children('tr').toArray()
categoryArray.map(category => {
console.log("category: " + category.name)
request(category.url).then( html => {
const $ = cheerio.load(html.data);
const items = $('table.table tbody').children('tr').toArray()
console.log("item.length: " + items.length)
items.map(item => {
request(item).then(html => {
const $ = cheerio.load(html.data);
const itemDetails = $('table.table tbody').children('tr').toArray()
console.log("item.name: " + itemDetails.name)
})
})
})
})
})
}
Но он не работает ...
console.log выглядит следующим образом:
category: A
category: B
category: C
category: D
category: E
category: F
category: G
category: H
category: I
category: J
category: K
category: L
category: M
category: N
item.length: 0
item.length: 100
item.length: 100
item.length: 0
item.length: 100
item.length: 0
item.length: 0
item.length: 100
item.length: 0
item.length: 0
item.length: 0
item.length: 0
item.length: 0
item.length: 0
item.name: item1
(node:5409) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
(node:5409) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
В первый раз выглядит нормально, но через несколько секунд не работает.
IПодумайте, что "categoryArray.map" не ожидает запросов детей.
Таким образом, номер потока HTTP-соединения максимально.
Я не знаю, как это исправить ...