2017-05-19 55 views
0

我使用nightmare.js刮公共记录,我只是想让刮板等待下一页加载。我正在抓取搜索结果,我按下了一个下一个按钮(显然)进入下一页。我无法使用nightmare.wait(someConstTime)准确地等待下一页加载,因为有时someConstTime比下一页加载所用的时间要短(尽管它总是在30秒以内)。我也不能使用nightmare.wait(selector),因为相同的选择器总是出现在所有结果页面上。在这种情况下,噩梦基本上不会等待,因为选择器已经存在(在我已经抓取的页面上),所以它会继续多次刮同一页面,除非新页面在下一个循环之前加载。点击链接后获取梦魇等待下一页加载

点击下一步按钮后,如何有条件地等待下一页加载?

如果我能想出如何 - 我的“显示#至##项#”比较当前页面(currentPageStatus)到最后一个已知值(lastPageStatus)的指示,等到它们是不同的(因此下一页加载)。

enter image description here (忽略的示例图像只有一个搜索结果页面)

我会做,使用从https://stackoverflow.com/a/36734481/3491991该代码但这需要经过lastPageStatusdeferredWait(我不能图出)。

这里是到目前为止,我已经得到了代码:

// 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变化。

在你的情况下,DOM改变元素是表CSS选择器:“#搜索结果”

我觉得MutationObserver是你所需要的。

我已经使用Mutation Summary库,它提供了MutationObservers的原始功能的一个很好的包装,以实现类似

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

的东西:从Tutorial

首先搜索结果被加载时注册MutationSummary观察员。

然后,点击“下一步”后,使用nightmare.evaluate等待mutationSummary回调函数返回提取的值。