2017-05-19 2 views
0

私はnightmare.jsを使用して公開レコードをスクラップし、次のページが読み込まれるまで待つようにしています。私は次のボタンを押して検索結果をクロールしています(明らかに)次のページに移動します。 someConstTimeは、次のページが読み込まれるまでの時間(常に30秒未満ですが)よりも短い場合があるため、次のページが読み込まれるのを正確に待つためにnightmare.wait(someConstTime)を使用することはできません。 nightmare.wait(selector)を使用することはできません。同じセレクタが常にすべての結果ページに存在するからです。この場合、悪夢は基本的にはセレクタが既に存在しているため(私がすでに掻き出したページ上に)、新しいページが次のループの前にロードされない限り、同じページを何回か削ります。リンクをクリックした後、次のページの読み込みを待つためにナイトメアを取得します

ボタンをクリックした後、次のページが読み込まれるのを条件付きで待つことはできますか?

私が現在のページ(currentPageStatus)の "#の##の##のエントリ"の表示を最後の既知の値(lastPageStatus)と比較し、それらが異なっているまで待つロードされた次のページ)。

enter image description here (たとえば、画像が一つだけの検索結果ページを持っていることを無視する)

私は(https://stackoverflow.com/a/36734481/3491991からこのコードを使用したが、それはdeferredWaitlastPageStatusを渡す必要になることは、私は理解できないことをやるだろうでる)。基本的に、あなたがロードされているページからの抽出を開始する前に、DOMの変更が完了する必要があり、私が理解できるものから

// Load dependencies 
//const { csvFormat } = require('d3-dsv'); 
const Nightmare = require('nightmare'); 
const fs = require('fs'); 
var vo = require('vo'); 

const START = 'http://propertytax.peoriacounty.org'; 
var parcelPrefixes = ["01","02","03","04","05","06","07","08","09","10", 
         "11","12","13","14","15","16","17","18","19"] 

vo(main)(function(err, result) { 
    if (err) throw err; 
}); 

function* main() { 
    var nightmare = Nightmare(), 
    currentPage = 0; 
    // Go to Peoria Tax Records Search 
    try { 
     yield nightmare 
     .goto(START) 
     .wait('input[name="property_key"]') 
     .insert('input[name="property_key"]', parcelPrefixes[0]) 
     // Click search button (#btn btn-success) 
     .click('.btn.btn-success') 
    } catch(e) { 
     console.error(e) 
    } 
    // Get parcel numbers ten at a time 
    try { 
     yield nightmare 
     .wait('.sorting_1') 
     isLastPage = yield nightmare.visible('.paginate_button.next.disabled') 
     while (!isLastPage) { 
      console.log('The current page should be: ', currentPage); // Display page status 
      try { 
      const result = yield nightmare 
       .evaluate(() => { 
       return [...document.querySelectorAll('.sorting_1')] 
        .map(el => el.innerText); 
       }) 
       // Save property numbers 
       // fs.appendFile('parcels.txt', result, (err) => { 
       // if (err) throw err; 
       // console.log('The "data to append" was appended to file!'); 
       // }); 
      } catch(e) { 
      console.error(e); 
      return undefined; 
      } 
      yield nightmare 
      // Click next page button 
      .click('.paginate_button.next') 
      // ************* THIS IS WHERE I NEED HELP *************** BEGIN 
      // Wait for next page to load before continue while loop 
      try { 
       const currentPageStatus = yield nightmare 
       .evaluate(() => { 
        return document.querySelector('.dataTables_info').innerText; 
       }) 
       console.log(currentPageStatus); 
      } catch(e) { 
       console.error(e); 
       return undefined; 
      } 
      // ************* THIS IS WHERE I NEED HELP *************** END 
      currentPage++; 
      isLastPage = yield nightmare.visible('.paginate_button.next.disabled') 
     } 
    } catch(e) { 
     console.error(e) 
    } 
    yield nightmare.end(); 
} 

答えて

0

は、ここで私はこれまで持っているコードです。あなたのケースでは

、DOMを変更するための要素は、CSSセレクタを持つテーブルである:私はMutationObserverは何が必要だと思う「#検索-結果」

私は似たような

var observer = new MutationSummary({ 
    callback: updateWidgets, 
    queries: [{ 
    element: '[data-widget]' 
    }] 
}); 

を達成するために、MutationObserversの生機能に素敵なラッパーを提供Mutation Summaryライブラリを使用しています:検索結果がロードされたときにTutorial

当初からMutationSummaryオブザーバを登録します。

次に、「次へ」をクリックした後、nightmare.evaluateを使用して、mutationSummaryコールバックが生成された値を返すのを待ちます。

関連する問題