Skip to content

Instantly share code, notes, and snippets.

@mherwig
Last active August 29, 2015 14:19
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 mherwig/cdf446154ac6ed02f671 to your computer and use it in GitHub Desktop.
Save mherwig/cdf446154ac6ed02f671 to your computer and use it in GitHub Desktop.
Example for web scraping using CasperJS/SlimerJS
// Usage: casperjs --ssl-protocol=any --engine=slimerjs paypal.js
// Open in browser: http://localhost:8083/get
var email = '',
password = '';
var casper = require('casper').create({
verbose: true,
logLevel: "debug"
}),
webserverTest = require("webserver").create();
webserverTest.listen(8083, function(request, response) {
if (request.url == '/get') {
casper.start();
casper.userAgent('Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.118 Safari/537.36');
casper.thenOpen('https://www.paypal.com/signin/', function() {
this.fill('form', {
'email': email,
'password': password
}, true);
});
casper.then(function() {
casper.waitForSelector('.balanceNumeral', function() {
response.statusCode = 200;
var myBalance = this.getHTML('div.balanceNumeral span');
var jsonData = {
balance: myBalance,
transactions: []
};
var transactionItems = this.getElementsInfo('div.transactionItem');
var item;
for (item in transactionItems) {
jsonData.transactions.push({
text: transactionItems[item].text
});
}
response.write(JSON.stringify(jsonData));
response.close();
});
});
casper.run(function() {});
}
});
@mherwig
Copy link
Author

mherwig commented Apr 21, 2015

I patched /usr/local/bin/casperjs so that I'll have a headless slimerjs with xvfb (sudo apt-get install xvfb):

#CASPER_COMMAND = [ENGINE_EXECUTABLE]

if ENGINE_EXECUTABLE == 'slimerjs':
    ENGINE_EXECUTABLE = 'xvfb-run slimerjs'

CASPER_COMMAND = ENGINE_EXECUTABLE.split(' ')

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment