Last active
March 12, 2017 20:07
-
-
Save akullpp/0c8a15a9aa1c104b93b6 to your computer and use it in GitHub Desktop.
nightmare: Alexa Top 500
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
// http://www.nightmarejs.org/ | |
var Nightmare = require('nightmare'); | |
var nightmare = Nightmare(); | |
var pages = Array.from({length:20}, (_, k) => (k)); | |
var top500 = []; | |
getTop500(pages); | |
function getTop500(pages) { | |
if (!pages.length) { | |
console.log(top500.join('\n')); | |
return nightmare.end() | |
}; | |
var page = pages.shift(); | |
return getTopsites(page) | |
.then(topsites => { | |
top500.push.apply(top500, topsites) | |
return getTop500(pages); | |
}); | |
} | |
function getTopsites(page) { | |
return nightmare | |
.goto(`http://www.alexa.com/topsites/global;${page}`) | |
.wait('.page-product-content') | |
.evaluate(function () { | |
var listings = document.querySelectorAll('.site-listing .desc-paragraph a'); | |
return Object.keys(listings).map(key => listings[key].innerText); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment