我的目标是下载网站的动态网页内容,因此需要在接收到的内容上执行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”,但不幸的是,它没有像使用网络浏览器那样加载完整的页面内容。如果有人能帮助我,我将不胜感激。
最佳答案
问题可能是您退出得太早了。尝试延迟脚本终止:
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/