Created
September 10, 2013 06:42
-
-
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
This file contains hidden or 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 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