我有一个脚本(如下),它通过 3 个步骤来抓取网站。一次最多设置为 1 页时效果很好。但是,当我一次将其增加到 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]