获取关注者数目
package com.web;
import java.util.List;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.JsonPathSelector;
public class FollowCountSpider implements PageProcessor {
/*
https://blog.csdn.net/community/home-api/v1/get-follow-list?page=1&size=100&noMore=false&blogUsername= + username
*/
private final String TAG = FollowCountSpider.class.getSimpleName();
Integer count = 0;
private Site site = Site
.me()
.setDomain("blog.csdn.net")
.setSleepTime(1000)
// 便于测试,休眠较长时间。
.setUserAgent(
"Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_2) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.1410.65 Safari/537.31");
@Override
public void process(Page page) {
String rawText = page.getRawText();
String data = new JsonPathSelector("$.data").select(rawText);
String replace = data.replace("{list=", "{\"list\":");
List selectList = new JsonPathSelector("$.list").selectList(replace);
count = selectList.size();
}
@Override
public Site getSite() {
return site;
}
}
测试类
/*
* 文件名称: T2.java
* 项目描述: ALIPAY 交易系统
* 公司名称: 杭州市阿里科技股份有限公司
* 版权所有: (C) 2022-2023
*/
package com.web;
import us.codecraft.webmagic.Spider;
/**
* @author globalcoding
* @since 2022/5/9 15:28
*/
public class T2 {
public static void main(String[] args) {
String username = "";
String pageIndex = "1";
int followCount = 0;
for (int i = 1; ; i++) {
String fanUrl =
"https://blog.csdn.net/community/home-api/v1/get-follow-list?page=" + i + "&size=20&noMore=false&blogUsername=" + username;
FollowCountSpider followCountSpider = new FollowCountSpider();
Spider.create(followCountSpider).addUrl(fanUrl).thread(1).run();
if (followCountSpider.count < 20) {
followCount = (i-1) * 20 + followCountSpider.count;
break;
}
}
System.out.println(followCount);
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)