Created
June 29, 2012 18:45
-
-
Save qcom/3019918 to your computer and use it in GitHub Desktop.
functioning casperjs scraper
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 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