通过PhantomJS检索文件 - 使用JSON.stringify可见

时间:2017-02-16 10:46:46

标签: javascript json xml phantomjs casperjs

我是使用PhantomJS的新手。

我使用简单的脚本来捕获URL。我测试了:

page.onResourceReceived = function (response) {
    console.log('Receive ' + JSON.stringify(response, undefined, 4));
};

我可以在输出中看到该文件可供接收。

Receive {
    "contentType": "text/xml; charset=utf-8",
    "headers": [
        {
            "name": "Cache-Control",
            "value": "private"
        },
        {
            "name": "Date",
            "value": "Thu, 16 Feb 2017 10:06:55 GMT"
        },
        {
            "name": "Transfer-Encoding",
            "value": "chunked"
        },
        {
            "name": "Content-Type",
            "value": "text/xml; charset=utf-8"
        },
        {
            "name": "Expires",
            "value": "0"
        },
        {
            "name": "Last-Modified",
            "value": "Thu, 16 Feb 2017 10:07:00 GMT"
        },
        {
            "name": "Content-Disposition",
            "value": "attachment; filename=\"Report.xml\""
        },
        {
            "name": "ID",
            "value": "0000Ld6OtL_7u1Qfy_FCD01OWlC5000^8O"
        },
        {
            "name": "X-Powered-By",
            "value": "Test"
        }
    ],
    "id": 14,
    "redirectURL": null,
    "stage": "end",
    "status": 200,
    "statusText": "OK",
    "time": "2017-02-16T10:07:00.759Z",
    "url": "http://xxx/xxx/xxx"
}

我有什么想法可以捕获文件 Report.xml ?我目前正在捕获页面的html,但不是它附带的XML文件。

干杯!

P.S。 这是我使用的代码:

var page = require("webpage").create();
var homePage = "http://url/report/this%20Report";

page.settings.javascriptEnabled = true;
page.settings.loadImages =false;


//page.open(homePage, function(status){
//  console.log("status="+status);
//  console.log("csv: " + page.plainText);
  //phantom.exit();
//});

page.onResourceRequested = function (request) {
    console.log('Request ' + JSON.stringify(request, undefined, 4));
};
page.onResourceReceived = function (response) {
    console.log('Receive ' + JSON.stringify(response, undefined, 4));
};

page.open(homePage, function (status) {
    if (status !== 'success') {
        console.log('Unable to load the address!');
        phantom.exit();
    } else {
        window.setTimeout(function () {
            console.log("csv: " + page.plainText);
            //page.render(output);
            phantom.exit();
        }, 10000); // Change timeout as required to allow sufficient time
    }
});

0 个答案:

没有答案