javascript Phantomjs 工作但速度很慢
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13144292/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Phantomjs works but is very slow
提问by Chris
I am trying to take a screenshot of a webpage with PhantomJS. Specifically, I am using the example of capturing espn.com
from thisexample. My code looks like this:
我正在尝试使用 PhantomJS 截取网页的屏幕截图。具体来说,我使用的是espn.com
从这个例子中捕获的例子。我的代码如下所示:
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. Is that normal? Is there a faster way to accomplish this so that it completes in a second or less?
一切运行良好,但是完成输出图像需要 6-8 秒。这是正常的吗?是否有更快的方法来完成此操作,使其在一秒或更短的时间内完成?
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 的内存、3.2 GHz 的 CPU,我在 speedtest.net 上的速度下降了 22Mbp/s,上升了 1Mbp/s
采纳答案by Layke
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.
It takes ~2 seconds for DOMContentLoaded to finish, and then ~7 seconds for the ready
event to fire.
看看当我espn.com
在系统上本地加载时会发生什么。DOMContentLoaded 需要大约 2 秒才能完成,然后ready
事件触发需要大约 7 秒。
回答by Devaroop
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. I could only figure it out when i used the remote debugger option.
好吧,就我而言,该页面正在等待一些 GET 请求,但无法访问请求的服务器,并且一直在等待很长时间。我只有在使用远程调试器选项时才能弄清楚。
phantomjs --remote-debugger-port=9000 loadspeed.js <some_url>
and inside the loadspeed.js file
在 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:
绕过这个 - 减少超时:
page.settings.resourceTimeout = 3000; //in milliseconds
and things were very quick after that. Hope this helps
之后的事情很快。希望这可以帮助
回答by ducci
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