javascript - 使用 PhantomJS 下载动态网页内容时遇到问题

标签 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”,但不幸的是,它没有像使用网络浏览器那样加载完整的页面内容。如果有人能帮助我,我将不胜感激。

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

最佳答案

问题可能是您退出得太早了。尝试延迟脚本终止:

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

关于javascript - 使用 PhantomJS 下载动态网页内容时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36966353/

相关文章:

javascript - 为每个评论区附加字符限制 jQuery

html - 如何使用 HTML 5 从服务器访问和下载文件

MATLAB - 'webread' 由于内容类型检查而变慢?

database-design - 基于角色的访问控制数据库设计

javascript - Node.js 控制台中的 `undefined` 是什么意思?

javascript - 模块中未找到函数

javascript - 动画滚动到 anchor 仅适用于第一个链接

wpf - 从 URL 同步下载图像

html - div 高度内的 div 的问题

javascript - 如何查看服务器发出的用户请求?