javascript
SpringBoot任务——JSoup+定时任务 定时爬取微博热搜至数据库
SpringBoot任務(wù)——JSoup+定時(shí)任務(wù) 定時(shí)爬取微博熱搜至數(shù)據(jù)庫(kù)
文章目錄
- SpringBoot任務(wù)——JSoup+定時(shí)任務(wù) 定時(shí)爬取微博熱搜至數(shù)據(jù)庫(kù)
- 0.前言
- 1.導(dǎo)入JSoup依賴
- 2.測(cè)試爬取微博熱搜
- 3. 配合定時(shí)任務(wù)注解實(shí)現(xiàn)定時(shí)爬取至數(shù)據(jù)庫(kù)
- 3.1 導(dǎo)入依賴與配置MySQL
- 3.2 熱搜實(shí)體類與對(duì)應(yīng)的數(shù)據(jù)表
- 3.3 使用MyBatisPlus寫實(shí)體類對(duì)應(yīng)的Mapper
- 3.4 @Scheduled注解實(shí)現(xiàn)定時(shí)執(zhí)行爬取
- 3.5 @EnableScheduling注解開(kāi)啟定時(shí)任務(wù)
- 3.6 運(yùn)行走起
0.前言
截至本文寫完:
- 微博熱搜的網(wǎng)址為:https://s.weibo.com/top/summary 如有變化,自行百度微博熱搜。
- HTML源碼中,熱搜數(shù)據(jù)在table標(biāo)簽中,而且第一個(gè)置頂?shù)臒崴褯](méi)有熱度。有時(shí)候還有推薦(應(yīng)該是廣告)
- 刷新后可能會(huì)有不同結(jié)果,幾率還挺大,我也是服了。
如下圖所示:
1.導(dǎo)入JSoup依賴
<!-- jsoup HTML解析庫(kù) --><!-- https://mvnrepository.com/artifact/org.jsoup/jsoup --><dependency><groupId>org.jsoup</groupId><artifactId>jsoup</artifactId><version>1.13.1</version></dependency>2.測(cè)試爬取微博熱搜
微博熱搜的網(wǎng)址: https://s.weibo.com/top/summary
Ctrl+U查看HTML源碼,可知熱搜數(shù)據(jù)以table顯示,Jsoup爬取table標(biāo)簽中的內(nèi)容,搜索一下便可…
我是參考這篇博客:[Java jsoup table 中獲取td和tr]( http://www.yq1012.com/myweb/2162.html )
由此可知爬取table標(biāo)簽中的內(nèi)容還是很簡(jiǎn)單的,在測(cè)試類中試試,代碼加注釋如下:
@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();//熱搜標(biāo)題String url = tds.get(1).select("a").attr("href");//熱搜URL網(wǎng)址(相對(duì)地址)String baseurl = "https://s.weibo.com";//和上述url組成完整可訪問(wèn)的單個(gè)熱搜URL//以 排名+熱搜標(biāo)題+熱搜指數(shù)+有效URL的形式輸出System.out.println(rank + " " + title + " " + num + " " + baseurl + url);}} catch (IOException e) {e.printStackTrace();}}測(cè)試結(jié)果如下:2020.03.02 21點(diǎn)44分爬取的微博熱搜:熱搜榜上只有50條,最上面的是置頂?shù)?#xff0c;沒(méi)有熱搜指數(shù)。接下來(lái)的定時(shí)任務(wù)中把這個(gè)去掉,那個(gè)for循環(huán)從1開(kāi)始即可。
28個(gè)省份恢復(fù)省際省內(nèi)道路客運(yùn) 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 韓國(guó)新冠肺炎定點(diǎn)醫(yī)院16名護(hù)士辭職 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 余文樂(lè) 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 馬云回贈(zèng)日本100萬(wàn)只口罩 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 ....其實(shí)關(guān)于熱搜的爬取到這就沒(méi)了,后面其實(shí)就是加了springboot中的定時(shí)任務(wù)和MybatisPlus實(shí)現(xiàn)了定時(shí)爬取熱搜至數(shù)據(jù)庫(kù)。
關(guān)于定時(shí)任務(wù)和MybatisPlus簡(jiǎn)單整合可查看:
SpringBoot任務(wù)——定時(shí)任務(wù)
SpringBoot數(shù)據(jù)訪問(wèn)——整合MybatisPlus
3. 配合定時(shí)任務(wù)注解實(shí)現(xiàn)定時(shí)爬取至數(shù)據(jù)庫(kù)
3.1 導(dǎo)入依賴與配置MySQL
這里用到了lombok插件+MybatisPlus+MySQL,導(dǎo)入相關(guān)依賴:
<!-- MySQL驅(qū)動(dòng)--><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 啟動(dòng)器--><dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.1.tmp</version></dependency>在application.proterties或者新建一個(gè)application.yml配置文件,配置MySQL數(shù)據(jù)源:
默認(rèn)的application.proterties
#mysql數(shù)據(jù)源配置 spring.datasource.driver-class-name=com.mysql.jdbc.Driver spring.datasource.url=jdbc:mysql://*.*.*.*:3306/數(shù)據(jù)庫(kù)名稱 spring.datasource.username=賬號(hào) spring.datasource.password=密碼yml格式:
#mysql數(shù)據(jù)源配置 spring:datasource:username: 賬號(hào)password: 密碼url: jdbc:mysql://*.*.*.*:3306/數(shù)據(jù)庫(kù)名稱driver-class-name: com.mysql.jdbc.Driver3.2 熱搜實(shí)體類與對(duì)應(yīng)的數(shù)據(jù)表
簡(jiǎn)單方便起見(jiàn),這里都用了字符串類型…我太懶了
實(shí)體類:
import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; //id 時(shí)間 排名 熱搜標(biāo)題 熱搜指數(shù) 有效URL @Data @AllArgsConstructor @NoArgsConstructor public class HotSearch {String id;//UUID生成String date;//時(shí)間格式:yyyy-MM-dd HH:mm:ssString rank;String title;String number;String url; }數(shù)據(jù)表:
3.3 使用MyBatisPlus寫實(shí)體類對(duì)應(yīng)的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注解實(shí)現(xiàn)定時(shí)執(zhí)行爬取
在之前的基礎(chǔ)上加了時(shí)間和ID,通過(guò)hotSearchMapper將數(shù)據(jù)插入數(shù)據(jù)當(dāng)中。
@Service public class MyJSoupService {@ResourceHotSearchMapper hotSearchMapper;//定時(shí)爬取微博熱搜 每天中午12點(diǎn)爬@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();//標(biāo)題String url = tds.get(1).select("a").attr("href");//熱搜詳細(xì)(標(biāo)題+熱度)String baseurl="https://s.weibo.com";//基址//時(shí)間SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//設(shè)置日期格式String date = df.format(new Date());// new Date()為獲取當(dāng)前系統(tǒng)時(shí)間,也可使用當(dāng)前時(shí)間戳//idString hotSearchId= UUID.randomUUID()+"";//以 id+時(shí)間+排名+熱搜標(biāo)題+熱搜指數(shù)+有效URL的形式輸出System.out.println("id:"+hotSearchId+" 時(shí)間"+date+" 排名"+rank+" 標(biāo)題"+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注解開(kāi)啟定時(shí)任務(wù)
別忘了使用@EnableScheduling注解標(biāo)注在類上開(kāi)啟定時(shí)任務(wù),這里標(biāo)注在的啟動(dòng)類上:
@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 運(yùn)行走起
我測(cè)試把時(shí)間調(diào)為22.52執(zhí)行了,結(jié)果如下:
控制臺(tái)正常輸出:
數(shù)據(jù)庫(kù)正常寫入50條熱搜數(shù)據(jù):
總結(jié)
以上是生活随笔為你收集整理的SpringBoot任务——JSoup+定时任务 定时爬取微博热搜至数据库的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 手机技巧:手机丢了记住这四步操作,让你的
- 下一篇: Spring:Spring相关知识介绍笔