javascript - Phantomjs 检查响应 header 然后执行某些操作

标签 javascript python ssl phantomjs

我有以下 RequestURL.js 文件。

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

page.customHeaders = {"pragma": "akamai-x-feo-trace"};
page.settings.userAgent = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36"

if (system.args.length === 1) {
    console.log('Try to pass some args when invoking this script!');
} else {
    page.open(system.args[1], function (status) {
    var content = page.content;
    console.log(content);
    phantom.exit();
    });
}

现在我将其执行为 phantomjs --ignore-ssl-errors=yes --ssl-protocol=any RequestURL.js #my_url_here > body.html

现在我有一个用 python 编写的解析器,它接受 body.html 并执行它。在此之前,我希望仅当响应包含以下 header 时才生成页面源。

X-Akamai-FEO-状态:转换

有没有办法修改我的 RequestURL.js 以到达那里。

最佳答案

预计page.onResourceReceivedpage.open()page.onLoadFinished 回调之前触发。

var transforming = false;
page.onResourceReceived = function(response){
    if (response.url === system.args[1]) { // TODO handle redirects if necessary
        response.headers.forEach(function(header){
            if(header.name === 'X-Akamai-FEO-State') {
               transforming = header.value === 'TRANSFORMING';
            }
        });
    }
};

page.open(system.args[1], function (status) {
    if (transforming) {
        console.log(page.content);
    }
    phantom.exit();
});

关于javascript - Phantomjs 检查响应 header 然后执行某些操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27896474/

相关文章:

javascript - 确定绝对或全局 z-index?

javascript - 怎么打开网站隐藏的信息

javascript - 如果我的脚本位于正文末尾,我应该使用 DOM 就绪函数吗?

python - 如何根据python中的键值计算字典的频率?

python - 动态读取csv文件python pandas

android - 如何在 Android 上正确配置 CharlesProxy

java - Ion w/Cloudflare free SSL(带 SNI)失败

JavaScript 运行时错误 : 'variable' is undefined while checking to see if undefined

python - 用 Popen 模仿 glib.spawn 异步...

java - 如何在我的应用程序中添加 SSL?