Skip to content

Instantly share code, notes, and snippets.

@qcom
Created June 29, 2012 18:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save qcom/3019918 to your computer and use it in GitHub Desktop.
Save qcom/3019918 to your computer and use it in GitHub Desktop.
functioning casperjs scraper
var skus = [113247,113248,113249];
var sku = [113247];
var casper = require('casper').create({
clientScripts: ['jQuery.js']
});
casper.start('http://bradyid.com', function() {
this.fill('form[action="/bradyid/catalog/siteSearchResultsView.do"]', { 'searchTerms': sku}, true);
});
var url = '';
casper.then(function() {
url = casper.evaluate(function($) {
return $('.imgCol a').attr('href');
});
});
casper.then(function() {
this.open(url).then(function() {
var obsolete = this.evaluate(function($) {
return Boolean(($('*:contains("Obsolete")')).length);
});
if (obsolete) { this.then(scrapeObsolete); }
else { this.then(scrape); }
});
});
function scrapeObsolete() {
console.log('Scraping Product: ' + this.getCurrentUrl());
var o = this.evaluate(function ($) {
var old = +($('.prdctDescription td:contains("Catalog #")').next().html())
var replacement = $('.tabContHolder p:contains("replacement") a').html().replace(/.$/, '');
return {
old: old,
obsolete: true,
replacement: replacement
}
});
console.log('old: ' + o.old);
console.log('replacement: ' + o.replacement + '\n');
}
function scrape() {
// to be soon implemented
console.log('Scraping Product: ' + this.getCurrentUrl());
}
casper.run();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment