日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

SpringBoot任务——JSoup+定时任务 定时爬取微博热搜至数据库

發(fā)布時間:2023/12/10 javascript 34 豆豆
生活随笔 收集整理的這篇文章主要介紹了 SpringBoot任务——JSoup+定时任务 定时爬取微博热搜至数据库 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

SpringBoot任務——JSoup+定時任務 定時爬取微博熱搜至數(shù)據(jù)庫

文章目錄

    • SpringBoot任務——JSoup+定時任務 定時爬取微博熱搜至數(shù)據(jù)庫
      • 0.前言
      • 1.導入JSoup依賴
      • 2.測試爬取微博熱搜
      • 3. 配合定時任務注解實現(xiàn)定時爬取至數(shù)據(jù)庫
        • 3.1 導入依賴與配置MySQL
        • 3.2 熱搜實體類與對應的數(shù)據(jù)表
        • 3.3 使用MyBatisPlus寫實體類對應的Mapper
        • 3.4 @Scheduled注解實現(xiàn)定時執(zhí)行爬取
        • 3.5 @EnableScheduling注解開啟定時任務
        • 3.6 運行走起

0.前言

截至本文寫完:

  • 微博熱搜的網(wǎng)址為:https://s.weibo.com/top/summary 如有變化,自行百度微博熱搜。
  • HTML源碼中,熱搜數(shù)據(jù)在table標簽中,而且第一個置頂?shù)臒崴褯]有熱度。有時候還有推薦(應該是廣告)
  • 刷新后可能會有不同結果,幾率還挺大,我也是服了。

如下圖所示:

1.導入JSoup依賴

<!-- jsoup HTML解析庫 --><!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.13.1</version></dependency>

2.測試爬取微博熱搜

微博熱搜的網(wǎng)址: https://s.weibo.com/top/summary

Ctrl+U查看HTML源碼,可知熱搜數(shù)據(jù)以table顯示,Jsoup爬取table標簽中的內(nèi)容,搜索一下便可…

我是參考這篇博客:[Java jsoup table 中獲取td和tr]( http://www.yq1012.com/myweb/2162.html )

由此可知爬取table標簽中的內(nèi)容還是很簡單的,在測試類中試試,代碼加注釋如下:

@Testvoid TestCrawlingHotSearch() {try {String urlStr = "https://s.weibo.com/top/summary";final Document doc = Jsoup.connect(urlStr).get();//獲取htmlElements trs = doc.select("tbody").select("tr");//獲取tbody下的所有tr下的html內(nèi)容for (org.jsoup.nodes.Element tr : trs) {Elements tds = tr.select("td");String rank = tds.get(0).text();//排名String num = tds.get(1).select("span").text();//熱度指數(shù)String title = tds.get(1).select("a").text();//熱搜標題String url = tds.get(1).select("a").attr("href");//熱搜URL網(wǎng)址(相對地址)String baseurl = "https://s.weibo.com";//和上述url組成完整可訪問的單個熱搜URL//以 排名+熱搜標題+熱搜指數(shù)+有效URL的形式輸出System.out.println(rank + " " + title + " " + num + " " + baseurl + url);}} catch (IOException e) {e.printStackTrace();}}

測試結果如下:2020.03.02 21點44分爬取的微博熱搜:熱搜榜上只有50條,最上面的是置頂?shù)?#xff0c;沒有熱搜指數(shù)。接下來的定時任務中把這個去掉,那個for循環(huán)從1開始即可。

28個省份恢復省際省內(nèi)道路客運 https://s.weibo.com/weibo?q=%2328%E4%B8%AA%E7%9C%81%E4%BB%BD%E6%81%A2%E5%A4%8D%E7%9C%81%E9%99%85%E7%9C%81%E5%86%85%E9%81%93%E8%B7%AF%E5%AE%A2%E8%BF%90%23&Refer=new_time 1 孫楊公布完整血樣瓶 6140146 https://s.weibo.com/weibo?q=%23%E5%AD%99%E6%9D%A8%E5%85%AC%E5%B8%83%E5%AE%8C%E6%95%B4%E8%A1%80%E6%A0%B7%E7%93%B6%23&Refer=top 2 高鑫去口罩廠做義工 3514533 https://s.weibo.com/weibo?q=%23%E9%AB%98%E9%91%AB%E5%8E%BB%E5%8F%A3%E7%BD%A9%E5%8E%82%E5%81%9A%E4%B9%89%E5%B7%A5%23&Refer=top 3 韓國新冠肺炎定點醫(yī)院16名護士辭職 2636260 https://s.weibo.com/weibo?q=%23%E9%9F%A9%E5%9B%BD%E6%96%B0%E5%86%A0%E8%82%BA%E7%82%8E%E5%AE%9A%E7%82%B9%E5%8C%BB%E9%99%A216%E5%90%8D%E6%8A%A4%E5%A3%AB%E8%BE%9E%E8%81%8C%23&Refer=top 4 余文樂 2355872 https://s.weibo.com/weibo?q=%E4%BD%99%E6%96%87%E4%B9%90&Refer=top 5 偶像失聲 2011436 https://s.weibo.com/weibo?q=%E5%81%B6%E5%83%8F%E5%A4%B1%E5%A3%B0&Refer=top 6 馬云回贈日本100萬只口罩 1627005 https://s.weibo.com/weibo?q=%23%E9%A9%AC%E4%BA%91%E5%9B%9E%E8%B5%A0%E6%97%A5%E6%9C%AC100%E4%B8%87%E5%8F%AA%E5%8F%A3%E7%BD%A9%23&Refer=top ....

其實關于熱搜的爬取到這就沒了,后面其實就是加了springboot中的定時任務和MybatisPlus實現(xiàn)了定時爬取熱搜至數(shù)據(jù)庫。
關于定時任務和MybatisPlus簡單整合可查看:
SpringBoot任務——定時任務
SpringBoot數(shù)據(jù)訪問——整合MybatisPlus

3. 配合定時任務注解實現(xiàn)定時爬取至數(shù)據(jù)庫

3.1 導入依賴與配置MySQL

這里用到了lombok插件+MybatisPlus+MySQL,導入相關依賴:

<!-- MySQL驅(qū)動--><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><scope>runtime</scope></dependency><!-- Lombok插件--><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency><!-- mybatis-plus 啟動器--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.1.tmp</version></dependency>

在application.proterties或者新建一個application.yml配置文件,配置MySQL數(shù)據(jù)源:

默認的application.proterties

#mysql數(shù)據(jù)源配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://*.*.*.*:3306/數(shù)據(jù)庫名稱 spring.datasource.username=賬號 spring.datasource.password=密碼

yml格式:

#mysql數(shù)據(jù)源配置 spring:datasource:username: 賬號password: 密碼url: jdbc:mysql://*.*.*.*:3306/數(shù)據(jù)庫名稱driver-class-name: com.mysql.jdbc.Driver

3.2 熱搜實體類與對應的數(shù)據(jù)表

簡單方便起見,這里都用了字符串類型…我太懶了

實體類:

import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //id 時間 排名 熱搜標題 熱搜指數(shù) 有效URL @Data @AllArgsConstructor @NoArgsConstructor public class HotSearch {String id;//UUID生成String date;//時間格式:yyyy-MM-dd HH:mm:ssString rank;String title;String number;String url; }

數(shù)據(jù)表:

3.3 使用MyBatisPlus寫實體類對應的Mapper

使其具備基本的CRUD功能。

import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.piao.springboot_scheduled_jsoup.Entity.HotSearch; import org.apache.ibatis.annotations.Mapper;@Mapper public interface HotSearchMapper extends BaseMapper<HotSearch> { }

3.4 @Scheduled注解實現(xiàn)定時執(zhí)行爬取

在之前的基礎上加了時間和ID,通過hotSearchMapper將數(shù)據(jù)插入數(shù)據(jù)當中。

@Service public class MyJSoupService {@ResourceHotSearchMapper hotSearchMapper;//定時爬取微博熱搜 每天中午12點爬@Scheduled(cron ="0 50 22 * * *")public void CrawlingHotSearch() {try {String urlStr = "https://s.weibo.com/top/summary";final Document doc = Jsoup.connect(urlStr).get();Elements trs = doc.select("tbody").select("tr");for (int i = 1; i < trs.size(); i++) {Elements tds = trs.get(i).select("td");String rank = tds.get(0).text();//排名String num = tds.get(1).select("span").text();//熱度指數(shù)String title = tds.get(1).select("a").text();//標題String url = tds.get(1).select("a").attr("href");//熱搜詳細(標題+熱度)String baseurl="https://s.weibo.com";//基址//時間SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設置日期格式String date = df.format(new Date());// new Date()為獲取當前系統(tǒng)時間,也可使用當前時間戳//idString hotSearchId= UUID.randomUUID()+"";//以 id+時間+排名+熱搜標題+熱搜指數(shù)+有效URL的形式輸出System.out.println("id:"+hotSearchId+" 時間"+date+" 排名"+rank+" 標題"+title+" 熱搜指數(shù)"+num+" 有效URL"+baseurl+url);hotSearchMapper.insert(new HotSearch(hotSearchId,date,rank,title,num,baseurl+url));//插入單條熱搜數(shù)據(jù)}} catch (IOException e) {e.printStackTrace();}} }

3.5 @EnableScheduling注解開啟定時任務

別忘了使用@EnableScheduling注解標注在類上開啟定時任務,這里標注在的啟動類上:

@EnableScheduling @SpringBootApplication @MapperScan("com.piao.springboot_scheduled_jsoup.mapper") public class SpringbootScheduledJsoupApplication {public static void main(String[] args) {SpringApplication.run(SpringbootScheduledJsoupApplication.class, args);}}

3.6 運行走起

我測試把時間調(diào)為22.52執(zhí)行了,結果如下:

控制臺正常輸出:

數(shù)據(jù)庫正常寫入50條熱搜數(shù)據(jù):

總結

以上是生活随笔為你收集整理的SpringBoot任务——JSoup+定时任务 定时爬取微博热搜至数据库的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。