使用PhantomJS下载动态Web内容时遇到问题

时间:2016-05-01 11:54:42

标签: javascript download web phantomjs

我的目标是下载网站的动态网页内容,因此需要对收到的内容执行javascript。我目前使用PhantomJS 2.1的代码如下:

var page = require('webpage').create();
var fs = require('fs');

page.open('https://sports.bovada.lv/soccer/premier-league', function () {
    page.includeJs("http://ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js", function () {
        page.evaluate(); // Edit: this line is removed
        page.close();
    });
});

page.onLoadFinished = function() {
    console.log("Download finished");
    fs.write('test.html', page.content, 'w');
    phantom.exit(0);
};

代码将收到的页面保存为“test.html”,但不幸的是,它没有像使用Web浏览器那样加载整页内容。如果有人可以帮助我,我将不胜感激。

用于测试的网站:https://sports.bovada.lv/soccer/premier-league

1 个答案:

答案 0 :(得分:1)

问题可能是你的退出太快了。尝试延迟脚本终止:

page.onLoadFinished = function() {
    console.log("Download finished");
    fs.write('test.html', page.content, 'w');
    setTimeout(function(){
        phantom.exit(0);
    }, 1000);
};
相关问题