本文介绍了PhantomJS page.open冻结的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用Capture示例使用PhantomJS打开网站

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();
});

使用Ubuntu 14.04,该代码在我的本地计算机上运行良好.但是,当我在虚拟服务器上运行代码时,永远不会调用回调,并且脚本不会退出.服务器和本地计算机运行的是完全相同的设置.我在使用VMWare虚拟化的2台不同服务器和使用Virtuozzo虚拟化的2台服务器上进行了尝试.

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.

推荐答案

实际上,UserAgent造成了所有麻烦. @ artjom-b向我指出了这个问题网站的可靠截图? Phantomjs和Casperjs都在某些网站上返回空屏幕截图.唯一的不同是,对我而言,从未创建图像文件.

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.

因此使用

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.

这篇关于PhantomJS page.open冻结的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 17:40