【问题标题】:Nodejs Request Parallel request with Bluebird responseNodejs请求与Bluebird响应的并行请求
【发布时间】:2017-12-16 15:51:42
【问题描述】:

我有两个 url,我一直在请求并使用 Bluebird Promise 库想用 Cheerio 处理 url 的 html。我似乎无法获得结果 html。我应该在内部传播中使用什么?

    let url1 = request('http://example1.com')
    let url2 = request('http://example2.com')

    Promise.all([url1, url2])
    .spread(function (url1RqRes, url2RqRes) {

        // How do I get access to the response html here ???

    })
    .catch(function (err) {
        console.log(err)
    });

【问题讨论】:

    标签: node.js request bluebird cheerio request-promise


    【解决方案1】:
    var cheerio = require('cheerio'); // Basically jQuery for node.js
    var rp = require('request-promise');
    
    let url1 = rp('http://example1.com')
    let url2 = rp('http://example2.com')
    
    Promise.all([url1, url2])
    .then(function (results) {
        console.log('results', results)
        let pages = results.map((resp) => cheerio.load(resp))
        // you will get the result in the same order of promise passed as array
    })
    .catch(function (err) {
        console.log(err)
    });
    

    供参考request-promise with cheerio

    【讨论】:

    • 感谢@zabusa,但我如何访问文档 html ?我通过发送请求加载。我的主要目标是将html加载到cheerio
    • @Deepankar console the results 你可以看到响应了吧?
    • 它只是整个请求对象。我无法访问文档正文
    • @Deepankar 你在使用哪些库?request-promise
    • m 从 Bluebird 导入 Promise,request from request
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多