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 + '
');
});
});
});
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 + '
');
});
});
}, function(err) {
console.log("Error with scraping r/nottheonion");
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…