Skip to content

Instantly share code, notes, and snippets.

@bdbai
Last active Aug 16, 2016
Embed
What would you like to do?
A simple instant web crawler server
var express = require('express');
var cheerio = require('cheerio');
var superagent = require('superagent');
var app = express();
app.get('/', (req, res) => {
Promise.all(Array.from(Array(42).keys()).map(i => // 从 0 到 41
new Promise((resolve, reject) => {
superagent.get('http://ipn.li/kernelpanic/' + (i+1).toString())
.end((err, sres) => {
if (err) {
reject(err);
} else {
var $ = cheerio.load(sres.text);
var result = $('body > main > article > header > h1 > a')[0].children[1].data;
res.write(result + '\r\n');
resolve();
}
});
})
)).then(() => {
res.end();
});
});
app.listen(3000, () => {
console.log('app is listening at port 3000');
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment