PhantomJS 多个页面的意外加载行为

2024-03-20

我有一个脚本(如下),可以通过 3 个步骤来抓取网站。当设置为一次最多一页时效果很好。然而,当我将其增加到一次 2 个时,事情开始变得不稳定。 onFinished 的触发时间比我预期的要早,并且页面尚未完全加载。因此,我的脚本的其余部分被破坏了。知道为什么会发生这种情况吗?我应该补充一点,我正在使用最新版本(1.5)。

MAX_PAGES = 1
### 
changing MAX_PAGES to >1 causes some pages onFinished event to fire before
the page is fully rendered.  this is evident by the fact that there are >1 images
for some pages.  i havent been able to reproduce using microsoft.com, but on some
pages i was working on the first onLoadFinished seemed to be called before the page
was actually fully loaded based on the look of the rendered images
###

newPage = (id) ->
context = {}
context.id = id
context.step = 0
context.page = require('webpage').create()
context.page.onLoadStarted = ->
    context.step++
context.page.onLoadFinished = (status) ->
    console.log status
    if status is 'success'
        context.page.render("#{context.id}_#{context.step}.png")
    else
        context.page.release()
        context.page.open('http://www.microsoft.com')
        console.log 'started loading'

newPage id for id in [1..MAX_PAGES]

我认为问题与 PhantomJS 中的每个网页都使用相同的 QNetworkAccessManager 这一事实有关,因此,完成的() http://doc.qt.nokia.com/4.7-snapshot/qnetworkaccessmanager.html#finished当每个网页对象完成加载时会触发信号。可能需要修改 PhantomJS 的代码才能解决此问题。我之前在尝试在 PhantomJS 中并行加载多个页面时注意到了这一点。我正在开发的一个应用程序使用 QtWebkit 并同时加载多个页面,因此我必须确保每个网页都有自己的 QNetworkAccessManager,以便 finish() 信号不会相互干扰。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

PhantomJS 多个页面的意外加载行为 的相关文章

随机推荐