简体   繁体   English

Phantomjs 工作但速度很慢

[英]Phantomjs works but is very slow

I am trying to take a screenshot of a webpage with PhantomJS.我正在尝试使用 PhantomJS 截取网页的屏幕截图。 Specifically, I am using the example of capturing espn.com from this example.具体来说,我使用的是从这个例子中捕获espn.com例子。 My code looks like this:我的代码如下所示:

var page = new WebPage(); 
    page.open('http://www.espn.com', function (status) {
    page.render('fb.png');
    phantom.exit();
});

I then go to my PhantomJS directory with either my terminal or command prompt and run:然后,我使用终端或命令提示符转到 PhantomJS 目录并运行:

phantomjs shotty.js

Everything runs great, however it takes 6-8 seconds to complete the output image.一切运行良好,但是完成输出图像需要 6-8 秒。 Is that normal?这是正常的吗? Is there a faster way to accomplish this so that it completes in a second or less?是否有更快的方法来完成此操作,使其在一秒或更短的时间内完成?

I am using CentOS and Windows 7. Both boxes have 8GB of RAM, 3.2 GHz CPU, and I'm getting 22Mbp/s down and 1Mbp/s up on speedtest.net我正在使用 CentOS 和 Windows 7。两个盒子都有 8GB 的​​ RAM、3.2 GHz CPU,我在 speedtest.net 上的速度下降了 22Mbp/s,上升了 1Mbp/s

Well, in my case, the page was waiting for some GET requests and was not able to reach the requests' server and it kept waiting for long.好吧,就我而言,该页面正在等待一些 GET 请求并且无法访问请求的服务器,并且一直在等待很长时间。 I could only figure it out when i used the remote debugger option.我只有在使用远程调试器选项时才能弄清楚。

phantomjs --remote-debugger-port=9000 loadspeed.js <some_url>

and inside the loadspeed.js file add this below code:并在loadspeed.js文件中添加以下代码:

page.onResourceRequested = function (req) {
    console.log('requested: ' + JSON.stringify(req, undefined, 4));
};

page.onResourceReceived = function (res) {
    console.log('received: ' + JSON.stringify(res, undefined, 4));
};

and then loading localhost:9000 in any webkit browser (safari/chrome) and seeing the console logs where i could figure out it was waiting for some unreachable requests for a long time.然后在任何 webkit 浏览器(safari/chrome)中加载 localhost:9000 并查看控制台日志,我可以发现它正在等待一些无法访问的请求很长时间。

TO BYPASS THIS - REDUCE THE TIMEOUT by adding below to the same loadspeed.js file: loadspeed.js这个 - 通过将以下添加到同一个loadspeed.js文件来减少超时:

page.settings.resourceTimeout = 3000; //in milliseconds

and things were very quick after that.之后的事情很快。 Hope this helps希望这可以帮助

Yes this is normal.是的,这是正常的。 When you attempt to render, PhantonJS will still wait for the page.open event to fire the load event to signify that the entire DOM has been loaded.当您尝试渲染时,PhantonJS 仍将等待page.open事件触发load事件以表示整个 DOM 已加载。

Take a look at what happens when I load espn.com locally on my system.看看当我在系统上本地加载espn.com时会发生什么。 It takes ~2 seconds for DOMContentLoaded to finish, and then ~7 seconds for the ready event to fire. DOMContentLoaded 需要大约 2 秒才能完成,然后ready事件需要大约 7 秒才能触发。

在此处输入图片说明

I didn't thought that the following would work but for me it did (on Windows):我不认为以下内容会起作用,但对我来说确实如此(在 Windows 上):

open Internet Explorer > Internet Options > Connections > LAN Settings and disable the "Automatically detect settings"打开 Internet Explorer > Internet 选项 > 连接 > LAN 设置并禁用“自动检测设置”

original Post: https://plus.google.com/+MatthiasG%C3%B6tzke/posts/9v9BMCJj2k6原帖: https : //plus.google.com/+MatthiasG%C3%B6tzke/posts/9v9BMCJj2k6

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM