首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Phantomjs没有在onResourceRequested中加载正确的url

Phantomjs没有在onResourceRequested中加载正确的url
EN

Stack Overflow用户
提问于 2016-09-29 07:50:29
回答 1查看 204关注 0票数 0

我有一个基于Phantomjs的网络监控示例的简单脚本,它试图记录与特定网站相关的所有请求和响应头:

代码语言:javascript
运行
复制
"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);


}

但是,当我试图使用./phantomjs getRequests.js www.google.com (或任何其他网页)运行它时,它会给出错误:

代码语言:javascript
运行
复制
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: []

基本上,我在终端上输入的url是以脚本文件的路径为主的。我在address变量中获得的输入非常好,即google.com。

有谁能帮忙解决这个问题吗?我不明白为什么幽灵会这么做。我正在VirtualBox上运行Ubuntu14.04。当我试图输出特定页面的请求时,脚本运行得很好。谢谢。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-09-29 08:17:27

你可能会笑,但是正确的使用脚本的方法是

代码语言:javascript
运行
复制
 ./phantomjs getRequests.js http://www.google.com
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39764405

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档