0

I have a simple script based off Phantomjs's Network Monitoring example that tries to log all the requests and the response headers associated with a particular website:

"use strict";

var page = require('webpage').create(),
    system = require('system'),
    address;

if (system.args.length === 1) {
    console.log('Usage: getRequests.js <some URL>');
    phantom.exit(1);
} else {

    address = system.args[1];

    console.log('The url entered is ' + address + '\n')

    page.onResourceReceived = function(response) {
        console.log('Received Request: ' + JSON.stringify(response.headers, undefined, 4) + '\n');
    };

    page.onResourceRequested = function (req) {
        console.log('Request URL ' + JSON.stringify(req.url, undefined, 4) + "\n");

    }

    page.onResourceError = function(resourceError) {
        console.log('Unable to load resource (#' + resourceError.id + 'URL:' + resourceError.url + ')');
        console.log('Error code: ' + resourceError.errorCode + '. Description: ' + resourceError.errorString);
    };

    page.open(address);


}

However, when I try to run it, using ./phantomjs getRequests.js www.google.com (or any other webpage), it gives the error:

The url entered is www.google.com

Request URL "file:///home/myusername/scripts/www.google.com"

Unable to load resource (#1URL:file:///home/myusername/scripts/www.google.com)
Error code: 203. Description: Error opening /home/myusername/scripts/www.google.com: No such file or directory
Received Request: []

Basically, the url that I am entering on the terminal is being prepended with the path of the script file. The input that I am getting inside the address variable is perfectly fine i.e. google.com

Could anyone please help with this issue? I cannot understand why phantomjs maybe doing this. I am running Ubuntu 14.04 on VirtualBox. And the script was running fine before when I was just trying to output the requests for a particular page. Thanks.

1 Answer 1

1

You'll probably laugh, but the correct way to use the script is

 ./phantomjs getRequests.js http://www.google.com
Sign up to request clarification or add additional context in comments.

1 Comment

Oh god, thank you so much!! I spent an hour on this.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.