Skip to content

Instantly share code, notes, and snippets.

@am
Created September 10, 2013 06:42
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 am/6505755 to your computer and use it in GitHub Desktop.
Save am/6505755 to your computer and use it in GitHub Desktop.
Page scarping with phantomjs: `phantomjs track.js XXXXXXXX output.txt` XXXXXXXX is a TNT tracking code
var page = require('webpage').create(),
args = require('system').args;
if (args.length === 1) {
console.log('Missing track ID, "phantomjs track.js XXXXXXXX"');
phantom.exit();
}
else {
args.forEach(function(arg, i) {
console.log('args > ' + i + ': ' + arg);
});
}
page.open('http://www.tnt.com/webtracker/tracking.do?cons='+args[1], function(status) {
console.log('status: ' + status);
// find the data in the response
var table = page.evaluate(function(s) {
return document.getElementsByClassName(s)[1].innerText;
}, 'appTable');
// show the result
console.log(table);
// if a file is specified in execute params
// write the output to the file
if (args[2]) {
var fs = require('fs');
try {
fs.write(args[2], table, 'w');
} catch(e) {
console.log(e);
}
}
phantom.exit();
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment