5

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]
4

2 に答える 2

4

問題は、PhantomJS内の各Webページが同じQNetworkAccessManagerを使用しているという事実に関係していると思います。したがって、各Webページオブジェクトの読み込みが完了すると、finished()シグナルが発生します。この問題を修正するには、PhantomJSのコードを変更する必要がある場合があります。PhantomJSで複数のページを並行してロードしようとしたときに、これに気づきました。私が取り組んでいるアプリケーションはQtWebkitを使用し、複数のページを同時にロードするため、finished()シグナルが互いに干渉しないように、各Webページが独自のQNetworkAccessManagerを取得するようにする必要があります。

于 2012-05-02T19:46:58.477 に答える
3

複数のページをクロールするには、ライブラリにバンドルされているfollow.jsの例を参照してください。https://github.com/ariya/phantomjs/blob/master/examples/follow.js

次のページをロードする前に、再帰を使用して現在のページがロードされるのを待つ必要があります。

于 2012-06-30T02:07:21.583 に答える