Javascript Phantom.js 是否捕获所有 AJAX?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14747643/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 17:47:11  来源:igfitidea点击:

Does Phantom.js capture all AJAX?

javascriptbackbone.jsphantomjs

提问by TIMEX

I have a backbone app that has a ton of AJAX. (each page load results in 5-6 ajax calls) because I have many views and internal views within those views,

我有一个包含大量 AJAX 的主干应用程序。(每个页面加载导致 5-6 次 ajax 调用)因为我在这些视图中有很多视图和内部视图,

if I run Phantom.js, can I get the final html result?

如果我运行 Phantom.js,我能得到最终的 html 结果吗?

Final result is what the user sees after all my ajax calls run.

最终结果是用户在我的所有 ajax 调用运行后看到的结果。

回答by Cybermaxs

PhamtonJs is a headless WebKit with JavaScript API. When crawling a page, PhatomJs will download all dependants requests such as images, js, css... It will also executes JS and ajax calls. However the onLoaded callback is invoked when the page finishes the loading, not when there are no pending requests. So, you have to use a trick for this.

PhamtonJs 是一个带有 JavaScript API 的无头 WebKit。在抓取页面时,PhatomJs 会下载所有依赖的请求,如图片、js、css……它还会执行 JS 和 ajax 调用。然而 onLoaded 回调是在页面完成加载时调用的,而不是在没有挂起的请求时调用。所以,你必须为此使用一个技巧。

There are two way to solve the problem :

有两种方法可以解决问题:

  • Wait until "something" effectively changed in you page (an element). The best example is waitFor.js
  • Wait a few seconds until no requests were received.
  • 等到“某事”在您的页面(一个元素)中有效更改。最好的例子是waitFor.js
  • 等待几秒钟,直到没有收到请求。

回答by sbridges

Phantom.js is able to run the javascript in your page, so this should work. There is an articledescribing how to give crawlers nice versions of javascript sites using Phantom.js.

Phantom.js 能够在您的页面中运行 javascript,所以这应该可以工作。有一篇文章描述了如何使用 Phantom.js 为爬虫提供不错的 javascript 站点版本。

Be sure to give Phantom.js enough time to complete the ajax calls, as this thread describes.

确保给 Phantom.js 足够的时间来完成 ajax 调用,如该线程所述

回答by Naga Kiran

You can use $.active along with waitFor.js script which returns the no of active connection to a server and we need to wait for it to be 0 for all requests to be complete.

您可以将 $.active 与 waitFor.js 脚本一起使用,该脚本返回到服务器的活动连接数,我们需要等待它为 0 才能完成所有请求。

More info: jQuery.active function

更多信息:jQuery.active 函数

回答by iplus26

If you have the access to javascript codes of the website, aside from waitFor()way, I think you can make your ajax requests synchronous. It's sometimes simpler and more accurate.

如果您可以访问网站的 javascript 代码,除此之外waitFor(),我认为您可以使您的 ajax 请求同步。有时它更简单、更准确。