each 数据获取attr_我背着CSDN偷偷记录了大半年我博客数据
作為一個(gè)數(shù)據(jù)控+一個(gè)有追求的技術(shù)博主,總是希望自己能知道自己博客歷史每日粉絲數(shù)量、閱讀量、積分、評(píng)論……的數(shù)據(jù),然而官方博客管理后臺(tái)給展示的數(shù)據(jù)太少了,只有每日訪問量、評(píng)論數(shù)、粉絲數(shù)、收藏?cái)?shù)這幾個(gè)數(shù)據(jù),而且目前最多只能看最近一個(gè)月的數(shù)據(jù)。
如果想看更久的數(shù)據(jù)、想看自己積分的變化、想看總排名 周排名的變化…… 沒有這些數(shù)據(jù),我作為技術(shù)博主年底的年終總結(jié)怎么辦?沒有這些數(shù)據(jù),我怎么知道自己長期是否進(jìn)步了,進(jìn)步速度又是什么樣的? 這當(dāng)然難不住一個(gè)優(yōu)秀的程序猿,本著沒有困難創(chuàng)造困難也要上的態(tài)度,我用Jsoup寫了個(gè)簡單的爬蟲,記錄了我博客2019年年底到現(xiàn)在每天的博客數(shù)據(jù)(除個(gè)別幾天因?yàn)椴┛透陌鎸?dǎo)致數(shù)據(jù)記錄異常)。沒錯(cuò),這個(gè)功能已經(jīng)上線大半年了,來先給大家展示下我的數(shù)據(jù)。jsoup是一款Java的HTML解析器,可以從html中解析數(shù)想要的數(shù)據(jù),是用java寫爬蟲必備的工具。
每日增量、總量數(shù)據(jù)隨意切換 閱讀量、粉絲量、評(píng)論數(shù)、點(diǎn)贊數(shù)、總排名、周排名…… 任意選取 隨意選取時(shí)間區(qū)間自從有了這個(gè)工具后,我博客一切數(shù)據(jù)盡收眼底,每天看著這數(shù)據(jù)一點(diǎn)點(diǎn)的變化,還是蠻有成就感、蠻開心的呢 !!
如何做?
秀完該告訴大家如何做的,首先你得有臺(tái)能執(zhí)行定時(shí)任務(wù)的主機(jī),云主機(jī)或者你臥室的主機(jī)都可以,然后得有個(gè)數(shù)據(jù)庫,至于整體功能其實(shí)就是一個(gè)簡單的增刪改查。哦 不對(duì),只有增查沒有刪改,數(shù)據(jù)展示的話我用了螞蟻金服開源的可視化庫antv g2,我用的3.8 bug很多不推薦,推薦使用highchart。
我認(rèn)為其中比較復(fù)雜的部分應(yīng)該是html數(shù)據(jù)解析的部分,這部分后面我會(huì)直接把我代碼告訴你。其次就是數(shù)據(jù)庫的存儲(chǔ)和查詢,我用spring-boot搭了個(gè)web服務(wù),用了spring-boot-starter-quartz寫了每天晚上11:55的定時(shí)任務(wù),用mybatis-spring-boot-starter來讀寫數(shù)據(jù)庫。
html的解析代碼,需要看懂csdn博客頁的html布局,然后逐漸調(diào)試獲取數(shù)據(jù),當(dāng)然csdn官方一改版,代碼就執(zhí)行不了了,所幸這種致命性改版頻率不會(huì)特別高,這大半年我就遇到過2-3次,代碼如下,可以直接拿來用,把url換成自己博客url就可以了。
public class CommonUtils {private static Logger log = LoggerFactory.getLogger(CommonUtils.class);private static Map<String, String> headers;static {headers = new HashMap<>();headers.put("referer", "https://www.google.com/");headers.put("User-Agent", "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0" +".4183.83 Safari/537.36");}public static BlogInfoDao getBlogInfo() {int retry = 3;while (--retry > 0) {try {BlogInfoDao blogInfoDao = new BlogInfoDao();blogInfoDao.setDate(new Date());Document doc = Jsoup.connect("https://blog.csdn.net/xindoo").headers(headers).get();Element blogElement = doc.getElementsByClass("data-info d-flex item-tiling").get(0);// 文章數(shù)量int articleCnt = Integer.parseInt(blogElement.getElementsByTag("dl").get(0).attr("title"));blogInfoDao.setArticleCnt(articleCnt);// 周排名int wranking = Integer.parseInt(blogElement.getElementsByTag("dl").get(1).attr("title"));blogInfoDao.setWranking(wranking);// 總排名int ranking = Integer.parseInt(blogElement.getElementsByTag("dl").get(2).attr("title"));blogInfoDao.setRanking(ranking);// 總閱讀量int viewCnt = Integer.parseInt(blogElement.getElementsByTag("dl").get(3).attr("title"));blogInfoDao.setViewCnt(viewCnt);blogElement = doc.getElementsByClass("data-info d-flex item-tiling").get(1);// 總積分int scoreCnt = Integer.parseInt(blogElement.getElementsByTag("dl").get(0).attr("title"));blogInfoDao.setScore(scoreCnt);// 粉絲數(shù)量int fansCnt = Integer.parseInt(blogElement.getElementsByTag("dl").get(1).attr("title"));blogInfoDao.setFansCnt(fansCnt);// 點(diǎn)贊量int likeCnt = Integer.parseInt(blogElement.getElementsByTag("dl").get(2).attr("title"));blogInfoDao.setLikeCnt(likeCnt);// 評(píng)論量int commentCnt = Integer.parseInt(blogElement.getElementsByTag("dl").get(3).attr("title"));blogInfoDao.setCommentCnt(commentCnt);// 收藏量int collectCnt = Integer.parseInt(blogElement.getElementsByTag("dl").get(4).attr("title"));blogInfoDao.setCollectCnt(collectCnt);return blogInfoDao;} catch (Exception e) {log.error("get bloginfo error, {}", e);}}return null;} }blogInfoDao是我封裝的用來和數(shù)據(jù)庫交互的類,沒啥內(nèi)容這里就不再貼了。
結(jié)語
我數(shù)據(jù)目前是半公開狀態(tài),可以讓大家看,https://xindoo.xyz/ 然后用github登陸后就可以看到數(shù)據(jù)了,有興趣可以體驗(yàn)下。之前有人建議開源,不過這個(gè)源碼是放在一個(gè)我個(gè)人項(xiàng)目中的,里面很多東西不適合公開,所以暫時(shí)不考慮了。
本文來自https://blog.csdn.net/xindoo
總結(jié)
以上是生活随笔為你收集整理的each 数据获取attr_我背着CSDN偷偷记录了大半年我博客数据的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: androidtabhost缓存_And
- 下一篇: hadoop元数据合并过程_Hadoop