Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
V2_THANKS
var Urls = function(startUrl){
this.startUrl = startUrl;
this.page = 0;
this.homePage = '';
this.heartsSent = 0;
this.heartsRecv = 0;
}
Urls.prototype.crawl = function(year){
var self = this;
var url = this.startUrl + this.page;
var end = false;
$.get(url, function(data, status){
self.page += 1
self.homePage = $(data);
if(year == undefined){
self.heartsRecv += self.homePage.find('.data tr td:nth-child(2):contains("收到谢意")').length;
self.heartsSent += self.homePage.find('.data tr td:nth-child(2):contains("发送谢意")').length;
end = self.homePage.find('.normal_page_right').first().hasClass('disable_now');
} else {
self.homePage.find('.data tr').each(function(i){
var tr = $(this);
var mDate = new Date(tr.find('td:nth-child(1)').text());
end = mDate < new Date(year+'-01-01');
if(!end && new Date(year+1+'-01-01') > mDate ){
self.heartsRecv += tr.find('td:nth-child(2):contains("收到谢意")').length;
self.heartsSent += tr.find('td:nth-child(2):contains("发送谢意")').length;
}
});
}
console.log(''+self.page+'');
if(!end){
self.crawl(year);
} else {
console.log('共收到谢意:'+self.heartsRecv);
console.log('共发送谢意:'+self.heartsSent);
}
});
}
new Urls('https://www.v2ex.com/balance?p=').crawl(2016);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.