-
-
Save donfanning/3c3e0d91791a7c82a72bcf0239ba5a33 to your computer and use it in GitHub Desktop.
my first attempt at a web crawler in node... obviously I have no idea how to use asynchronicity
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var http = require("http"); | |
var fs = require("fs"); | |
var crawler = require("simplecrawler"); | |
var cheerio = require("cheerio"); | |
var util = require('util'); | |
var exit = require('exit'); | |
var log = console.log.bind(console); | |
function str(a) {return util.inspect(a, false, null);} | |
var url = "https://gist.github.com/matthewmorrone1?page=3"; | |
crawler.initialProtocol = "https"; | |
crawler.maxDepth = 1; | |
var out = 0; | |
crawler.crawl(url, function(queueItem, responseBuffer, response){ | |
log(url); | |
if(out === 1) { | |
exit(); | |
} | |
var $ = cheerio.load(responseBuffer); | |
var links = $("a").map(function() { | |
return [$(this).find("span strong").html(), +$(this).attr("href")]; | |
}).filter(function() { | |
return this[0]; | |
}); | |
out++; | |
fs.writeFile("out.txt", /*str(queueItem)+"\n\n"+*/Array.prototype.join.call(links, "\n")/*+"\n\n"+str(response)*/, function(err) { | |
if(err) { | |
log(err); | |
} else { | |
log("The file was saved!"); | |
} | |
}); | |
}, function(queueItem) { | |
log("fail"); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment