Javascript 如何在Node.js中按顺序发送多个http请求?

Javascript 如何在Node.js中按顺序发送多个http请求?,javascript,node.js,web-crawler,Javascript,Node.js,Web Crawler,我正在使用Node.js创建一个爬虫程序 在目标网页中,有10多个类别 我可以用我的爬虫抓到它们 我对每个类别都提出了要求。(10多个请求) 然后,每个类别页面有100多个项目 我对每一项都提出要求。(100多个请求) 所以我需要10+*100+个请求 我的密码在这里 const axios = require("axios") const cheerio = require("cheerio"); async function request(url) { return await

我正在使用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.
第一次,它看起来工作正常,但几秒钟后,就不工作了

我认为“categoryArray.map”不会等待孩子们的请求

因此,HTTP连接线程数已达到最大值


我不知道如何修复它…

您的问题是
Array.prototype.map
不支持
Promise
,因此它无法等待您的请求

不要使用
映射
,只需使用
异步
/
等待
并使用
对数组进行迭代即可。。。属于

async function main() {
    const categoryArray = await request(categoryUrl)
    for (const category of categoryArray) {
        console.log("category: " + category.name)

        const items = await request(category.url)
        console.log("item.length: " + items.length)

        for (const item of items) {
            const itemDetails = await request(item)
            console.log("item.name: " + itemDetails.name)
        }
    }
}

查看async或使用队列(调用完成后,从阵列中弹出下一个队列并重复)它如何工作?你有错误吗?这是怎么一回事?有人在打网络电话吗?请详细说明你所面临的问题。如果它不起作用呢?请详细说明,“不起作用”是什么意思?
request()
函数的代码是什么?查看Promise.all谢谢!我更改了代码,它看起来是按顺序工作的。但我还有一个问题。。。我编辑了我的问题,你能再帮我一次吗?@yoonhok据我所知,你的更新答案没有问题,至少在这个代码方面是这样。看起来很简单,您请求的结果给出了一个空数组,请看。