给朋友写的,顺便也分享出来
/**
* Created by wplct on 2017-08-28.
*/
// 是否关注
var host = 'http://www.jianshu.com';
if(!!$('.author .btn.btn-success.follow').length){
// 关注
$.post(host+'/users/6446857/toggle_like',{fuckuc: "1"});
console.log('关注成功');
}else {
console.log('已经关注');
}
// 获取作者首页
var author_url = $('.author .avatar').attr('href');
// 获取作者文章数据
var re = /<a class="title" target="_blank" href="(\/p\/.*?)">/g;
var ps = [];
var like_re = /"is_liked_note":true/g;
var find_like = /data-note-id="\d*?"/g;
function like() {
if(!ps.length){
console.log('完工');
return;
}
var a = ps.pop();
var url = a.slice(39,a.length-2);
$.get(host+url,{},function (text) {
// console.log(text);
var arr = text.match(like_re);
if(!arr){
var note_id = text.match(find_like)[0];
note_id = note_id.slice(14,note_id.length-1);
$.post(host+'/notes/'+note_id+'/like',{},function () {
console.log('喜欢成功'+note_id);
});
setTimeout('like()',1000)
}else {
console.log('已经喜欢');
setTimeout('like()',1000)
}
})
}
function getP(page) {
$.get(host+author_url+'?order_by=shared_at&page='+page,{},function (text) {
// console.log(text)
var arr = text.match(re);
// console.log(arr);
if(!!arr){
console.log('爬取到'+arr.length+'篇文章');
$.merge(ps,arr);
setTimeout("getP("+(page+1)+")",1000)
}else {
console.log("文章爬取完成")
like();
}
});
}
getP(1);
单行压缩版
var host="http://www.jianshu.com";if(!!$(".author .btn.btn-success.follow").length){$.post(host+"/users/6446857/toggle_like",{fuckuc:"1"});console.log("关注成功")}else{console.log("已经关注")}var author_url=$(".author .avatar").attr("href");var re=/<a class="title" target="_blank" href="(\/p\/.*?)">/g;var ps=[];var like_re=/"is_liked_note":true/g;var find_like=/data-note-id="\d*?"/g;function like(){if(!ps.length){console.log("完工");return}var b=ps.pop();var c=b.slice(39,b.length-2);$.get(host+c,{},function(e){var a=e.match(like_re);if(!a){var d=e.match(find_like)[0];d=d.slice(14,d.length-1);$.post(host+"/notes/"+d+"/like",{},function(){console.log("喜欢成功"+d)});setTimeout("like()",1000)}else{console.log("已经喜欢");setTimeout("like()",1000)}})}function getP(a){$.get(host+author_url+"?order_by=shared_at&page="+a,{},function(c){var b=c.match(re);if(!!b){console.log("爬取到"+b.length+"篇文章");$.merge(ps,b);setTimeout("getP("+(a+1)+")",1000)}else{console.log("文章爬取完成");like()}})}getP(1);
简明使用方法 按F12 在输入框中粘贴后回车