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

Having trouble downloading dynamic web content with PhantomJS

本文关键字:遇到 问题 web PhantomJS 下载 动态 使用      更新时间:2023-09-26

我的目标是下载网站的动态web内容,因此需要对接收到的内容执行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

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

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