本篇內(nèi)容主要講解“jsoup怎么用”,感興趣的朋友不妨來看看。本文介紹的方法操作簡單快捷,實(shí)用性強(qiáng)。下面就讓小編來帶大家學(xué)習(xí)“jsoup怎么用”吧!
創(chuàng)新互聯(lián)建站服務(wù)項(xiàng)目包括茂南網(wǎng)站建設(shè)、茂南網(wǎng)站制作、茂南網(wǎng)頁制作以及茂南網(wǎng)絡(luò)營銷策劃等。多年來,我們專注于互聯(lián)網(wǎng)行業(yè),利用自身積累的技術(shù)優(yōu)勢、行業(yè)經(jīng)驗(yàn)、深度合作伙伴關(guān)系等,向廣大中小型企業(yè)、政府機(jī)構(gòu)等提供互聯(lián)網(wǎng)行業(yè)的解決方案,茂南網(wǎng)站推廣取得了明顯的社會效益與經(jīng)濟(jì)效益。目前,我們服務(wù)的客戶以成都為中心已經(jīng)輻射到茂南省份的部分城市,未來相信會繼續(xù)擴(kuò)大服務(wù)區(qū)域并繼續(xù)獲得客戶的支持與信任!
jsoup是一款Java的HTML解析器,可以從html中解析數(shù)想要的數(shù)據(jù),是用java寫爬蟲必備的工具。



自從有了這個工具后,我博客一切數(shù)據(jù)盡收眼底,每天看著這數(shù)據(jù)一點(diǎn)點(diǎn)的變化,還是蠻有成就感、蠻開心的呢 !!
秀完該告訴大家如何做的,首先你得有臺能執(zhí)行定時任務(wù)的主機(jī),云主機(jī)或者你臥室的主機(jī)都可以,然后得有個數(shù)據(jù)庫,至于整體功能其實(shí)就是一個簡單的增刪改查。哦 不對,只有增查沒有刪改,數(shù)據(jù)展示的話我用了螞蟻金服開源的可視化庫antv g2,我用的3.8 bug很多不推薦,推薦使用highchart。
我認(rèn)為其中比較復(fù)雜的部分應(yīng)該是html數(shù)據(jù)解析的部分,這部分后面我會直接把我代碼告訴你。其次就是數(shù)據(jù)庫的存儲和查詢,我用spring-boot搭了個web服務(wù),用了spring-boot-starter-quartz寫了每天晚上11:55的定時任務(wù),用mybatis-spring-boot-starter來讀寫數(shù)據(jù)庫。
html的解析代碼,需要看懂csdn博客頁的html布局,然后逐漸調(diào)試獲取數(shù)據(jù),當(dāng)然csdn官方一改版,代碼就執(zhí)行不了了,所幸這種致命性改版頻率不會特別高,這大半年我就遇到過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);
// 評論量
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)容這里就不再貼了。
到此,相信大家對“jsoup怎么用”有了更深的了解,不妨來實(shí)際操作一番吧!這里是創(chuàng)新互聯(lián)網(wǎng)站,更多相關(guān)內(nèi)容可以進(jìn)入相關(guān)頻道進(jìn)行查詢,關(guān)注我們,繼續(xù)學(xué)習(xí)!
分享名稱:jsoup怎么用
路徑分享:http://www.chinadenli.net/article42/iigsec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供ChatGPT、自適應(yīng)網(wǎng)站、服務(wù)器托管、外貿(mào)建站、小程序開發(fā)、電子商務(wù)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)