Java爬蟲(chóng)范例之使用Htmlunit爬取學(xué)校教務(wù)網(wǎng)課程表信息
使用WebClient和htmlunit實(shí)現(xiàn)簡(jiǎn)易爬蟲(chóng)
import com.gargoylesoftware.htmlunit.WebClient;
提供了public
P getPage(final String url)方法獲得HtmlPage。
import com.gargoylesoftware.htmlunit.html.*;
包含了HtmlPage、HtmlForm、HtmlTextInput、HtmlPasswordInput、HtmlElement、DomElement等元素。
構(gòu)造webclient對(duì)象
WebClient webClient= new WebClient();
無(wú)參默認(rèn)是BrowserVersion.BEST_SUPPORTED,有參構(gòu)造支持5種瀏覽器:
BrowserVersion.CHROME
BrowserVersion.EDGE
BrowserVersion.FIREFOX
BrowserVersion.FIREFOX_78
BrowserVersion.INTERNET_EXPLOER
使用webclient.getPage(String url)獲得頁(yè)面:
try { page = webClient.getPage(url); } catch (IOException e) { e.printStackTrace(); }
利用webClient.getPage(url);方法,將其封裝成一個(gè)getHtmlPage靜態(tài)方法
private static class innerWebClient{ private static final WebClient webClient = new WebClient(); } public static HtmlPage getHtmlPage(String url){ //調(diào)用此方法時(shí)加載WebClient WebClient webClient = innerWebClient.webClient; webClient.getOptions().setCssEnabled(false); //配置webClient webClient.getOptions().setCssEnabled(false); //設(shè)置CSS是否生效 webClient.getOptions().setJavaScriptEnabled(true); //設(shè)置JS是否生效 webClient.setAjaxController(new NicelyResynchronizingAjaxController()); //設(shè)置AJAX請(qǐng)求 webClient.getOptions().setThrowExceptionOnFailingStatusCode(false); //設(shè)置是否拋出異常碼 webClient.getOptions().setThrowExceptionOnScriptError(false); //設(shè)置是否拋出腳本錯(cuò)誤 webClient.waitForBackgroundJavaScript(3*1000); //設(shè)置等待JS毫秒數(shù) webClient.getCookieManager().setCookiesEnabled(true); //設(shè)置是否支持Cookie HtmlPage page = null; try { page = webClient.getPage(url); } catch (IOException e) { e.printStackTrace(); } return page; }
在教務(wù)官網(wǎng)學(xué)期課表頁(yè),拿到對(duì)應(yīng)標(biāo)簽的ID
登錄教務(wù)官網(wǎng)頁(yè)面:
靜態(tài)解析課程信息方法:
//獲取周次集合 public static ArrayList<Integer> getWeekCount(String weekAndSection){ ArrayList<Integer> weekList = new ArrayList<>(); int index = weekAndSection.indexOf("(周)"); if(index == -1){ return new ArrayList<>(); } String subWeek = weekAndSection.substring(0, index); //1-3,5,15,18 String[] weekArr = new String[10]; int idx = subWeek.indexOf(","); //1或3 int num = 0,n = 0; while (subWeek.contains(",")){ weekArr[num] = subWeek.substring(0,idx); //第一個(gè)逗號(hào)前面的內(nèi)容,給數(shù)組 subWeek = subWeek.substring(idx+1); //剩余內(nèi)容 n = subWeek.indexOf(","); idx = n; num++; } weekArr[num] = subWeek; for (String s : weekArr) { if(s!=null && !s.equals("")){ if(s.contains("-")){ int ix = s.indexOf("-"); int begin = Integer.parseInt(s.substring(0,ix)); int end = Integer.parseInt(s.substring(ix+1)); for (int i = begin; i <= end; i++) { weekList.add(i); } }else{ weekList.add(Integer.parseInt(s)); } } } return weekList; }
//獲取節(jié)次集合 public static ArrayList<Integer> getSectionCount(String weekAndSection){ int begin = weekAndSection.indexOf("[") + 1; int end = weekAndSection.indexOf("節(jié)"); String section = weekAndSection.substring(begin, end); int len = section.length(); String first = section.substring(0,2); String last = section.substring(len-2,len); ArrayList<Integer> sectionList = new ArrayList<>(); int firstInt = Integer.parseInt(first); int lastInt = Integer.parseInt(last); for (int i = firstInt; i <= lastInt; i++) { sectionList.add(i); } return sectionList; }
開(kāi)始解析課程信息
DomElement[][] domElements = new DomElement[7][6]; //7天,6個(gè)節(jié)次部分 String key = ""; //星期一~星期日:1-2~7-2 for (int i = 0;i < 7;i++){ //星期一到星期日 for (int j = 0;j <= 5;j++){ //sectionIds[0]到sectionIds[5] if(j == 2){ //由于第5節(jié)為空,略過(guò) continue; } key = sectionIds[j] + "-" + (i+1) + "-2"; if(page3.getElementById(key) == null){ throw new NullPointerException("Key過(guò)期了!"); }else{ domElements[i][j] = page3.getElementById(key); } String course = domElements[i][j].asText(); String temp[] = new String[10]; int num = 0; int index; for (int g = 0; course.contains("---------------------"); g = g + index) { index = course.indexOf("---------------------"); temp[num] = course.substring(0,index); course = course.substring(index+21); num++; } temp[num] = course; String[] courseInfo = new String[4]; for (int k = 0;k < temp.length;k++) { if(temp[k] == null || temp[k].equals("") || temp[k].equals(" ")){ continue; } if(temp[k].indexOf("\n") == 1){ temp[k] = temp[k].substring(2); } ArrayList<Integer> weekList; ArrayList<Integer> sectionList; if(temp[k].contains("網(wǎng)絡(luò)課")){ temp[k] = temp[k].substring(0,temp[k].indexOf("\n")); courseInfo[0] = temp[k]; weekList = null; sectionList = null; }else{ int idx,cnum = 0; for(int h = 0; temp[k].contains("\n") && cnum <= 3;h = h+idx){ idx = temp[k].indexOf("\n"); courseInfo[cnum] = temp[k].substring(0,idx); temp[k] = temp[k].substring(idx+1); cnum++; } weekList = getWeekCount(courseInfo[2]); sectionList = getSectionCount(courseInfo[2]); } System.out.println("課程名===" + courseInfo[0]); System.out.println("教師名===" + courseInfo[1]); System.out.println("周次===" + weekList); System.out.println("節(jié)次===" + sectionList); System.out.println("地點(diǎn)===" + courseInfo[3]); System.out.println("星期" + (i+1)); } } }
輸出效果:
基于Uni-App實(shí)現(xiàn)的課程表小程序:
以上就是Java爬蟲(chóng)范例之使用Htmlunit爬取學(xué)校教務(wù)網(wǎng)課程表信息的詳細(xì)內(nèi)容,更多關(guān)于Java 爬蟲(chóng)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
springboot將mybatis升級(jí)為mybatis-plus的實(shí)現(xiàn)
之前項(xiàng)目工程用的是mybatis,現(xiàn)在需要將其替換為mybatis-plus,本文主要介紹了springboot將mybatis升級(jí)為mybatis-plus的實(shí)現(xiàn),具有一定的參考價(jià)值,感興趣的可以了解一下2023-09-09詳解Java執(zhí)行g(shù)roovy腳本的兩種方式
這篇文章主要介紹了Java執(zhí)行g(shù)roovy腳本的兩種方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04Java并發(fā)中的ABA問(wèn)題學(xué)習(xí)與解決方案
這篇文章主要介紹了Java并發(fā)中的ABA問(wèn)題學(xué)習(xí)與解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-05-05Java找不到或無(wú)法加載主類(lèi)及編碼錯(cuò)誤問(wèn)題的解決方案
今天小編就為大家分享一篇關(guān)于Java找不到或無(wú)法加載主類(lèi)及編碼錯(cuò)誤問(wèn)題的解決方案,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-02-02Springboot?RestTemplate設(shè)置超時(shí)時(shí)間的簡(jiǎn)單方法
學(xué)習(xí)springboot ,RestTemplate的使用場(chǎng)景非常非常多,比如springcloud中的服務(wù)消費(fèi),下面這篇文章主要給大家介紹了關(guān)于Springboot?RestTemplate設(shè)置超時(shí)時(shí)間的簡(jiǎn)單方法,需要的朋友可以參考下2022-01-01一篇文章幫你搞懂什么是java的進(jìn)程和線(xiàn)程
這篇文章主要介紹了java 線(xiàn)程詳解及線(xiàn)程與進(jìn)程的區(qū)別的相關(guān)資料,網(wǎng)上關(guān)于java 線(xiàn)程的資料很多,對(duì)于進(jìn)程的資料很是,這里就整理下,需要的朋友可以參考下2021-08-08詳解SpringBoot中關(guān)于%2e的Trick
這篇文章主要介紹了SpringBoot中關(guān)于%2e的Trick,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-04-04