Last active
May 1, 2024 21:12
-
-
Save dineshsprabu/cebec20417da7868ff9b7350e4749e31 to your computer and use it in GitHub Desktop.
[PhantomJS] Load and save dynamic webpage
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
#!/usr/bin/phantomjs | |
// This script will load a dynamic page and gives you the content. | |
// Args: url -> URL to be downloaded. | |
var system = require('system'); | |
if(system.args.length == 1){ | |
system.stderr.write("Pass URL as argument to the script"); | |
phantom.exit(1) | |
} | |
url = system.args[1] | |
var page = require('webpage').create(); | |
page.settings.userAgent = 'Mozilla/5.0 (X11; Ubuntu; Linux i686; rv:13.0) Gecko/20100101 Firefox/13.0'; | |
page.onResourceError = function(resourceError) { | |
page.reason = resourceError.errorString; | |
page.reason_url = resourceError.url; | |
}; | |
page.onLoadFinished = function(status) { | |
if(status == "success"){ | |
system.stdout.write(page.content); | |
phantom.exit(); | |
} | |
}; | |
page.open(url, function(status){ | |
if (status !== 'success'){ | |
system.stderr.write('Unable to access network'); | |
system.stderr.write("Error opening url" + page.reason_url + ":" + page.reason); | |
phatom.exit(1); | |
} | |
else{ | |
system.stdout.write(page.content); | |
phantom.exit(); | |
} | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment