Nodejs爬蟲遍歷節點組裝Promis.all代碼片段

注意:jQuery的map返回的對象是一個jQuery對象包裝,需要通過get獲得裏面的值

var $ = cheerio.load(html);
promiseList = $('#semester').children('option').filter().map((index, element) => {
            menu_options.path = constant.gread_path_prefix + gloable_variables.student_id + '?semester=' + $(element).attr('value')//TODO
            return new Promise(function (resolve, reject) {//獲得學期列表
                http.request(menu_options, function (res) {
                    chunks = []
                    size = 0
                    res.on('data', function (chunk) {
                        chunks.push(chunk);
                        size += chunk.length;
                    });
                    res.on('end', function () {
                        buffer = Buffer.concat(chunks, size);
                        zlib.gunzip(buffer, function (err, decoded) {
                            resolve(decoded.toString())
                        })
                    });
                }).end();
            })
        }).get()

        Promise.all(promiseList).then(function (resules) {
            resules.forEach(function (content_html) {
                var $ = cheerio.load(content_html);
            })
        })
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章