【问题标题】:Phantomjs check for response headers and then execute somethingPhantomjs 检查响应标头,然后执行某些操作
【发布时间】:2015-01-12 06:27:56
【问题描述】:

我有以下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 并执行它。现在,在此之前,我希望仅当响应包含以下标头时才生成页面源。

X-Akamai-FEO-State:TRANSFORMING

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

【问题讨论】:

    标签: javascript python ssl phantomjs


    【解决方案1】:

    预计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();
    });
    

    【讨论】:

    • 丑但有效。希望可以做得更合理。
    • 我不明白这有多难看。如果您能详细说明困扰您的问题,也许可以改进。
    猜你喜欢
    • 2011-11-13
    • 1970-01-01
    • 2018-11-05
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-16
    • 2012-02-25
    相关资源
    最近更新 更多