噩梦js浏览和parsing多个网站

我正在尝试使用github.com/segmentio/nightmare自动化2个站点

例:

  1. 我想去site_1 (临时电子邮件服务)并获得一封电子邮件。
  2. 然后转到site_2并填写该电子邮件,然后提交表单。
  3. 然后去site_1查看电子邮件是否到达
  4. parsing激活链接的电子邮件
  5. 然后点击该链接并再次转到site_2

………..等

是噩梦js和node.js好这种浏览器操纵?

因为对于官方API等简单的东西没关系:

var Nightmare = require('nightmare'); new Nightmare() .goto('http://yahoo.com') .type('input[title="Search"]', 'github nightmare') .click('.searchsubmit') .run(function (err, nightmare) { if (err) return console.log(err); console.log('Done!'); }); 

那复杂的东西呢? 谢谢

在这里我创build一个函数来获取不同条件的HTML源,我爬行TimeWarnerCable页面获取有关电视,互联网和捆绑计划的信息,所以我的函数获取一些参数,并在不同的调用中对每个参数作出反应:

 function getSource(url,serviceQuantity,zip){ var defer=Q.defer(); var Nightmare = require('nightmare'); var nightmare = Nightmare({openDevTools:browserDev ,show: browserVisible,'webPreferences':{partition: 'nopersist'}}); nightmare .goto(url) .cookies.clear() .wait(2000) .exists('div.messagebox-wrapper.twc-container[style="display: block;"]') .then(function(noZipSet){ if (noZipSet){ debug('No zipcode settled down'); nightmare .insert('fieldset > div > input[placeholder="Enter Your ZIP Code"]',zip) .type('fieldset > div > input[placeholder="Enter Your ZIP Code"]', '\u000d');//I do "Enter" because nightmare can't find the submit button }else{ debug('Zipcode settled down'); nightmare .click('div.section.newHeaderIcons > div > ul > li:nth-child(4) > div > a') .wait(2000) .insert('form.geoLoc > fieldset > div > input[placeholder="Update Your ZIP Code"]',zip) .type('form.geoLoc > fieldset > div > input[placeholder="Update Your ZIP Code"]', '\u000d');//I do "Enter" because nightmare can't find the submit button } nightmare .wait(8500) .exists('div[style="display: block;"] > div > div > div > div > div > div > div.parsys.oof-error-content > div > div > div > div > div > div > p[style="color: #333333;"]') .then(function(zipNotAvailable){ if (zipNotAvailable){ debug('Service not available in '+zip+' for '+serviceQuantity+' services'); nightmare .end() .then(function(){ defer.resolve(''); }); }else{ debug('Service available on the zipcode'); switch (serviceQuantity) { case 1: nightmare .evaluate(function(){ return document.querySelector('html').innerHTML; }) .end() .then(function (result) { defer.resolve(result); }) .catch(function (error) { debug('ERROR >> Search failed:', error); }); break; case 2: nightmare .click('#tv-filter') .wait(500) .click('#internet-filter') .wait(500) .evaluate(function(){ return document.querySelector('html').innerHTML; }) .end() .then(function (result) { defer.resolve(result); }) .catch(function (error) { debug('ERROR >> Search failed:', error); }); break; case 3: nightmare .click('#tv-filter') .wait(500) .click('#internet-filter') .wait(500) .click('#phone-filter') .wait(500) .evaluate(function(){ return document.querySelector('html').innerHTML; }) .end() .then(function (result) { defer.resolve(result); }) .catch(function (error) { debug('ERROR >> Search failed:', error); }); break; } } }); }); return defer.promise; } 

恶梦现在使用Electron而不是PhantomJS,API与以前不同。 文档仅显示如何使用发电机。 以下是我如何做,而不使用生成器,并使用承诺。 完成后,您可以使用这三个命令杀死电子浏览器。

此外,确保您validationDOM操作的每一步(使用受控recursion),并在操作之间留出大量的等待时间。 否则,你不会总是得到你所期望的。

 var Nightmare = require('nightmare'), Promise = require('q').Promise; var nightmare = new Nightmare({show: true}); function goToSite1(data){ Promise.resolve(nightmare .goto('http://stackoverflow.com/questions/32518777/nightmare-js-browsing-and-parsing-multiple-sites') .evaluate(function(){ return $('#question-header > h1 > a').text(); })).then(function(html){ goToSite2(html); }); } function goToSite2(data){ Promise.resolve(nightmare .goto('https://github.com/segmentio/nightmare') ).then(function(){ nightmare.proc.disconnect(); nightmare.proc.kill(); nightmare.ended = true; doOtherStuff(data); }) } function doOtherStuff(data){ console.log(data) } goToSite1();