本文介绍了NodeJS:麻烦用promise抓取两个URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在抓取r/theonion并将标题写入文本文件onion.txt.之后,我打算抓取r/nottheonion并将标题写入文本文件nottheonion.txt.我成功写入了onion.txt,但未写入nottheonion.txt.

I'm scraping r/theonion and writing the titles to a text file, onion.txt. After that, I am intending to scrape r/nottheonion and writing the titles to a text file, nottheonion.txt. I succeed in writing to onion.txt, but not to nottheonion.txt.

var onion_url = "https://www.reddit.com/r/theonion";
var not_onion_url = "https://www.reddit.com/r/nottheonion";

var promise = new Promise(function(resolve, reject) {

    request(onion_url, function(error, response, html) {
        if (error) {
            console.log("Error: " + error);
        }

        var $ = cheerio.load(html);

        $("div#siteTable > div.link").each(function(idx) {
            var title = $(this).find('p.title > a.title').text().trim();
            console.log(title);

            fs.appendFile('onion.txt', title + '\n');
        });
      });
    });

promise.then(function(result) {
    request(not_onion_url, function(error, response, html) {
        if (error) {
            console.log("Error: " + error);
        }

        var $ = cheerio.load(html);

        $("div#siteTable > div.link").each(function(idx) {
            var title = $(this).find('p.title > a.title').te .   xt().trim();
            console.log(title);

            fs.appendFile('not_onion.txt', title + '\n');
        });
     });
}, function(err) {
    console.log("Error with scraping r/nottheonion");
});

推荐答案

使用 request-promise fs-promise 来简化代码,如果您仍然想使用promise,并使用避免重复自己的功能.

Use request-promiseand fs-promise to simplify your code if you want to use promises anyway, and use function to not repeat yourself.

var rp = require('request-promise');
var fsp = require('fs-promise');

var onion_url = "https://www.reddit.com/r/theonion";
var not_onion_url = "https://www.reddit.com/r/nottheonion";

function parse(html) {
    var result = '';
    var $ = cheerio.load(html);
    $("div#siteTable > div.link").each(function(idx) {
        var title = $(this).find('p.title > a.title').text().trim();
        console.log(title);
        result += title + '\n';
    });
    return result;
}

var append = file => content => fsp.appendFile(file, content);

rp(onion_url)
  .then(parse)
  .then(append('onion.txt'))
  .then(() => console.log('Success'))
  .catch(err => console.log('Error:', err));

rp(not_onion_url)
  .then(parse)
  .then(append('not_onion.txt'))
  .then(() => console.log('Success'))
  .catch(err => console.log('Error:', err));

这未经测试.

这篇关于NodeJS:麻烦用promise抓取两个URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 16:22