本文是小编为大家收集整理的关于PhantomJS page.open冻结了的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。
问题描述
我正在尝试使用phantomjs使用捕获示例
打开网站var page = require('webpage').create(); page.open('http://github.com/', function() { page.render('github.png'); phantom.exit(); });
使用Ubuntu 14.04,该代码在我的本地计算机上正常工作.但是,当我在虚拟服务器上运行代码时,从未调用回调,并且脚本不会退出.服务器和我的本地计算机正在运行完全相同的设置.我在虚拟化的VMware和Virtuozzo的虚拟化上尝试了2个不同的服务器.
.这似乎不是网络错误,因为所有请求都是成功的.
推荐答案
实际上useragent造成了所有麻烦. @artjom-b向我指出了这个问题网站可靠的屏幕截图? phantomjs和casperjs都在某些网站上返回空屏幕拍摄.唯一的区别是对我来说,图像文件从未创建.
因此,用
设置用户page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36';
实际解决了问题.
问题描述
I am trying to open a website using PhantomJS using the Capture example
var page = require('webpage').create(); page.open('http://github.com/', function() { page.render('github.png'); phantom.exit(); });
The code works fine on my local machine, using Ubuntu 14.04. But when I run the code on a virtual server, the callback is never called and the script does not exit. The server and my local machine are running the exact same setup. I tried it on 2 different servers on virtualised with VMWare and one with Virtuozzo.
It does not seem to be a network error as all requests are successful.
推荐答案
Actually the UserAgent caused all the trouble. @artjom-b pointed me to this Question Taking reliable screenshots of websites? Phantomjs and Casperjs both return empty screen shots on some websites. The Only difference is that for me the image file was never created.
So setting the UserAgent with
page.settings.userAgent = 'Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36';
actually solved the problem.