Created
February 11, 2017 09:15
-
-
Save taisang1996/6f9eb3d10b86c767c51bd29d6b4b312f to your computer and use it in GitHub Desktop.
Scraping link thegioididong with ajax :)
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
var casper = require('casper').create({ | |
waitTimeout: 100000, | |
viewportSize: { | |
width: 900, | |
height: 720, | |
} | |
}); | |
var links; | |
function getLinks() { | |
// Scrape the links from top-right nav of the website | |
var links = document.querySelectorAll('.cate li > a'); | |
return Array.prototype.map.call(links, function (e) { | |
// return e.getAttribute('href') | |
return e.href | |
}); | |
} | |
function viewMore(max, i) { | |
i = i || 0; | |
if ((max == null || i < max) && this.visible('.viewmore')) { | |
this.thenClick('.viewmore'); | |
this.waitUntilVisible('.viewmore', null, function () {}); | |
this.then(function() { | |
this.capture('thegioididong_viewmore_'+i+'.png'); | |
viewMore.call(this, max, i+1); | |
}); | |
} | |
} | |
// Opens casperjs homepage | |
casper.start('https://www.thegioididong.com/dtdd'); | |
casper.then(function () { | |
viewMore.call(casper); | |
}); | |
casper.then(function () { | |
links = this.evaluate(getLinks); | |
}); | |
casper.run(function () { | |
for(var i in links) { | |
console.log(links[i]); | |
} | |
console.log(links.length); | |
casper.done(); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Run that