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);