如何使用phantomjs查看来自子请求的响应?

时间:2019-01-21 16:00:55

标签: javascript phantomjs

抱歉,我来自乌克兰。 我从php中使用phantomjs,get_end_html.js:

var system = require('system');
var args = system.args;

var page = require('webpage').create();
page.settings.userAgent = 'Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0';
page.viewportSize = {
  width: 1920,
  height: 1080
};

page.open(args[1], function (status) {
        var ua = page.evaluate(function () {

            return location.href+'##############'+document.getElementsByTagName('html')[0].innerHTML
        });
        console.log(ua);

        page.render('test.png');

        phantom.exit();

});

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

page.onResourceReceived = function(response) {
  console.log('Response (#' + response.id + ', stage "' + response.stage + '"): ' + JSON.stringify(response));
};

page.onError = function (msg, trace) {
  console.log(msg);
  trace.forEach(function(item) {
    console.log('  ', item.file, ':', item.line);
  });
};

在php中,我使用类似这样的东西:

$google_url = 'https://translate.google.com/#en/ru/translate%20this';
shell_exec('/usr/local/bin/phantomjs /get_end_html.js ' . $google_url);

然后我可以看到这样的请求:

Request { "headers": [ { "name": "Referer", "value": "https://translate.google.com/" }, { "name": "Origin", "value": "https://translate.google.com" }, { "name": "User-Agent", "value": "Mozilla/5.0 (X11; Linux i686; rv:64.0) Gecko/20100101 Firefox/64.0" }, { "name": "Content-Type", "value": "application/x-www-form-urlencoded;charset=UTF-8" }, { "name": "Accept", "value": "*/*" }, { "name": "Content-Length", "value": "2261" } ], "id": 11, "method": "POST", "postData": "q=%D0%9C%D0%B8%D1%80%20%D0%B4%D0%BE%D0%B1%D1%80%D0%B0%2C%20%D0%B1%D0%BE%D0%BB%D1%8C%20%D1%81%D0%B2%D0%BE%D0%B4%D1%8F%20%D0%BD%D0%B0%20%D0%BD%D0%B5%D1%82.%0A%D0%A1%D1%87%D0%B0%D1%81%D1%82%D1%8C%D1%8F%20%D0%BC%D0%B8%D0%B3%2C%20%D1%81%D0%BE%D0%BB%D0%BD%D1%86%D0%B0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97%D0%B2%D0%B5%D0%B7%D0%B4%D0%BE%D0%B9%2C%20%D1%81%D0%BE%D0%B3%D1%80%D0%B5%D1%82%D0%B0%D1%8F%20%D0%B7%D0%B5%D0%BC%D0%BB%D1%8F.%0A%D0%98%20%D0%BC%D1%8B%20%D0%BF%D0%BE%20%D0%BD%D0%B5%D0%B9%20%D0%B8%D0%B4%D0%B5%D0%BC.%0A%D0%98%20%D0%BC%D1%8B%20%D0%BF%D0%BE%20%D0%BD%D0%B5%D0%B9%20%D0%B8%D0%B4%D0%B5%D0%BC.%0A%0A%D0%98%20%D0%BF%D1%83%D1%81%D0%BA%D0%B0%D0%B9%20%D1%87%D1%82%D0%BE-%D1%82%D0%BE%20%D0%B2%D0%B4%D1%80%D1%83%D0%B3%20%D0%BD%D0%B5%20%D1%82%D0%B0%D0%BA%2C%0A%D0%A2%D1%8B%20%D0%B7%D0%B0%D0%B1%D1%83%D0%B4%D1%8C%2C%20%D0%BF%D1%80%D0%BE%D0%B4%D0%BE%D0%BB%D0%B6%D0%B0%D0%B9%20%D0%B8%D0%B4%D1%82%D0%B8.%0A%D0%92%D0%B5%D0%B4%D1%8C%20%D0%BB%D1%8E%D0%B1%D0%B0%D1%8F%20%D0%BF%D1%80%D0%BE%D0%B1%D0%BB%D0%B5%D0%BC%D0%B0%20-%20%D0%BF%D1%83%D1%81%D1%82%D1%8F%D0%BA.%0A%D0%98%20%D0%B2%D1%81%D0%B5%D0%B3%D0%B4%D0%B0%20%D0%BC%D0%BE%D0%B6%D0%BD%D0%BE%20%D0%B2%D1%8B%D1%85%D0%BE%D0%B4%20%D0%BD%D0%B0%D0%B9%D1%82%D0%B8.", "time": "2019-01-21T15:56:03.347Z", "url": "https://translate.google.com/translate_a/single?client=webapp&sl=ru&tl=en&hl=en&dt=at&dt=bd&dt=ex&dt=ld&dt=md&dt=qca&dt=rw&dt=rm&dt=ss&dt=t&source=bh&ssel=0&tsel=0&kc=1&tk=205364.372211"
}

据我所知,phantomjs会执行此请求,但看不到响应。是否可以看到来自此类型子请求的响应?谢谢。

0 个答案:

没有答案