【问题标题】:PhantomJs can't render a specific page from sourcePhantomJs 无法从源渲染特定页面
【发布时间】:2017-02-13 13:05:27
【问题描述】:

我已经成功地获取了原始 html(已通过其他产品检索到),然后让 phantomjs 获取该原始 html 并呈现整个页面,包括运行任何/所有 javascript。我最近遇到了一个没有呈现其 javascript 的页面。

这就是我运行它的方式...

phantomjs myscript.js > OUTPUT.txt 2>&1

这是演示问题的 myscript.js 文件...

var page = require('webpage').create(),
var system = require('system');
var address = 'http://cloud.firebrandtech.com/#!/login';
var rawHtml = '<!DOCTYPE html>\
<html>\
<head>\
    <meta charset="utf-8">\
<meta http-equiv="X-UA-Compatible" content="IE=edge">\
<meta name="viewport" content="width=device-width, initial-scale=1.0">\
<meta name="description" content="Web Portal for managing Cloud Products, Assets, and Distributions">\
<meta name="author" content="Firebrand Technologies">\
<title>Firebrand Cloud</title>\
<link rel="stylesheet" href="/widgets/css/widgets.css">\
<link rel="stylesheet" href="/css/portal.css">\
</head>\
<body ng-app="portal" fc-app="cloud" fc-direct="true" class="fc">\
    <div>\
        <div data-ng-if="user.isLoaded" data-ng-controller="PortalCtrl">\
            <div data-ng-include="getView()"></div>\
            <div class="container">\
                <div data-ui-view></div>\
            </div>\
        </div>\
    </div>\
    <script src="/widgets/js/widgets.js"></script>\
<script src="/js/vendor.js"></script>\
<script src="/js/portal.js"></script>\
</body>\
</html>';

page.settings.resourceTimeout = 5000;
page.settings.loadImages = false;
page.setContent(rawHtml, address);
window.setTimeout(function () {
    if(page.content.indexOf('Sign In') > -1)
        console.log('YAY!!! Javascript Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
    else
        console.log('BOO!!! Javascript NOT Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!')     

    phantom.exit();
}, 5000);

似乎此页面需要一些 auth/cors 才能工作。如果 phantomjs 发出实际请求(使用 page.open)来获取源,如下例所示,我可以让它工作。但是,此解决方案对我不起作用。 Phantomjs 必须使用上面示例中的源代码(就像我提到的,它对所有其他站点都很好用)。

var page = require('webpage').create(),
var system = require('system');
var address = 'http://cloud.firebrandtech.com/#!/login ';

page.open(address, function(status) {  
    setTimeout(function(){
        if(page.content.indexOf('Sign In') > -1)
            console.log('YAY!!! Javascript Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
        else
            console.log('BOO!!! Javascript NOT Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!')     

        phantom.exit();
    }, 5000)  
});

我已经尝试过使用如下标志,但它们似乎没有效果......

phantomjs --web-security=false --ignore-ssl-errors=true thefilebelow.js > OUTPUT.txt 2>&1

【问题讨论】:

    标签: phantomjs


    【解决方案1】:

    终于搞定了……

    由于我使用另一个产品(不是 phantomjs)来检索页面源,因此我需要保留随该请求发回的 cookie。然后我不得不像这样使用addCookie 传递这些cookie...

    var page = require('webpage').create(),
    var system = require('system');
    var address = 'http://cloud.firebrandtech.com/#!/login';
    var rawHtml = 'same raw html as above...';
    
    //THE NEXT 3 LINES ARE WHAT CHANGED
    var cookiesFromInitialRequest = [{name: 'aaa', value: 'bbb', domain: 'ccc'}, etc...]
    for(var i = 0; i < cookiesFromInitialRequest.length; i++)
        phantom.addCookie(cookiesFromInitialRequest[i])
    
    page.settings.resourceTimeout = 5000;
    page.settings.loadImages = false;
    page.setContent(rawHtml, address);
    window.setTimeout(function () {
        if(page.content.indexOf('Sign In') > -1)
            console.log('YAY!!! Javascript Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!')
        else
            console.log('BOO!!! Javascript NOT Rendered!!!!!!!!!!!!!!!!!!!!!!!!!!')     
    
        phantom.exit();
    }, 5000);
    

    【讨论】:

    • 那么...这是您问题的答案吗?
    • 是的,直到明天我才能选择它作为答案。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多