【问题标题】:Phantomjs: certain pages failing to openPhantomjs:某些页面无法打开
【发布时间】:2014-08-19 19:46:10
【问题描述】:

我目前正在编写一个涉及一些网络抓取的网络应用程序。为了解决这个问题,我正在使用 phantomjs 的帮助。但是,某些(但不是全部)网页返回 status="fail"。

这是代码(注意:这实际上是用 nodejs 编写的,使用此处找到的 node-phantom 库:https://github.com/alexscheelmeyer/node-phantom。虽然语法可能不同,但该库实际上直接与 phantomjs 一起使用,所以它不应该这样做有什么不同:

phantom.create(function (err,ph) {
    ph.createPage(function (err,page) {
        page.onResourceError = function(errorData) {
            console.log('Unable to load resource (URL:' + errorData.url + ')');
            console.log('Error code: ' + errorData.errorCode + '. Description: ' + errorData.errorString);
        };
        page.onLoadFinished = function(status) {
            console.log('Status: ' + status);
            if(status==='success') {
                page.includeJs('http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js', function () {
                    if(fetch_results) {
                        //THIS IS WHERE YOU WILL DO RESULTS SHIT
                        console.log("results page stuff entered");
                        page.render('phantomjs-test2.png');
                        ph.exit();
                    } else {
                        page.evaluate(function () {
                            //page evaluate stuff
                        }, function(err, result) {
                            console.log("entering here");
                            page.render('phantomjs-test.png');
                            if(!err) fetch_results = true;
                        });
                    }
                });
            } else {
                console.log(
                    "Error opening url \"" + page.reason_url
                    + "\": " + page.reason
                );
                console.log("Connection failed.");
                ph.exit();
            }
        }
        //page.open("https://www.google.com",function (err,status) {});
        page.open("https://www.pavoterservices.state.pa.us/Pages/PollingPlaceInfo.aspx",function (err,status) {});
    });
}, {parameters:{'ignore-ssl-errors':'yes'}});

因此,对于使用 google.com 的 page.open,页面加载成功。但是,在列出其他 url 时,它会返回以下错误:

 Unable to load resource (URL:https://www.pavoterservices.state.pa.us/Pages/PollingPlaceInfo.aspx);  Error code: 2. Description: connection closed;  Error opening url "undefined": undefined

任何关于为什么 google 会加载但没有列出的 url 的帮助将不胜感激!

【问题讨论】:

标签: javascript node.js phantomjs


【解决方案1】:

(注意:我在Issue trying to use PhantomJS to process a web page的回答完全一样)

尝试使用 --ssl-protocol=any 调用 phantomjs

我遇到了同样的问题,一个星期前可以使用的外部网站。

于是我进行了搜索,发现Qt QNetworkReply connection closed 中描述的相关问题。它帮助我查看了 phantomjs 的嵌入式 Qt:它默认强制 SSLv3 中的新连接,这对于旧站点来说太新了,或者对于新站点来说太旧了(但在 Qt 4.8.4 时这是一个相当合理的默认设置)发布)。

使用“any”,您告诉 phantomjs 尝试所有协议,这应该可以帮助您通过测试。它将尝试比 SSLv3 更安全的协议,但也比 SSLv3 更安全(SSLv3 处于中等范围)。因此,如果“any”有效,那么您应该尝试强制使用比 SSLv3 更安全的值,而不是让“any”。就我而言,指定 --ssl-protocol=tlsv1 有效。

猜猜最近 SSL 的问题(goto fail、heartbleed、poodle 等)导致很多网站升级他们的服务器,现在拒绝 SSLv3 连接。 但如果您的服务器使用旧于 SSLv3 的协议,请保留“任何”(以及所有相关的安全风险......)。

【讨论】:

  • 这应该是“答案”。现在建议在大多数服务器上阻止 SSLv3,设置 protocol=any 标志完美地解决了我的问题。谢谢!
  • 这救了我的命。谢谢!
【解决方案2】:

这会起作用。

var phantom = require('phantom');


phantom.create(function(ph) {
  ph.createPage(function(page) {
    page.open('https://www.facebook.com/login.php',
      function(status) {
        console.log('Opened site? %s', status);
        page.render("page.png");
        if (status !== 'success')
         {
        console.log('FAIL to load the address');
        }
        else
        {
        console.log('Success in fetching the page');
        another_funny(page, ph);
        ph.exit();
        }
     });
  });

}, {parameters:{'ssl-protocol':'any'}} );

function another_funny(page, ph) {
        console.log("like page");
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-01
    • 2010-10-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多