Skip to content

Instantly share code, notes, and snippets.

@tylerpearson
Created May 24, 2015 01:59
Show Gist options
  • Select an option

  • Save tylerpearson/7c118f638758619a1b5d to your computer and use it in GitHub Desktop.

Select an option

Save tylerpearson/7c118f638758619a1b5d to your computer and use it in GitHub Desktop.
var fs = require('fs');
var request = require('request');
var cheerio = require('cheerio');
console.log("Starting scraping");
var allResults = [];
for (var chapter=1; chapter <= 168; chapter++) {
(function(chapter) {
var chapterURL = "http://www.ncleg.net/EnactedLegislation/Statutes/HTML/ByChapter/Chapter_" + chapter + ".html";
// be nice and spread this out
setTimeout(function() {
request(chapterURL, function(error, response, html){
if (!error) {
var $ = cheerio.load(html);
var title = $('.aChapter').text();
if (title.length) {
var chapter = title.split('.')[0].trim(),
name = title.split('.')[1].trim(),
results = {};
results.chapter = chapter;
results.name = name;
console.log(JSON.stringify(results));
allResults.push(allResults);
// console.log(chapter);
// console.log(name);
} else {
// console.log("No title for " + chapterURL);
}
} else {
console.log("Error loading " + chapterURL);
}
})
}, Math.random() * 20000);
}(chapter));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment