/** * 进行借书信息抓取操作 * * @param spider - 已登录的Spider * @throws ClientProtocolException * @throws IOException * @return 借阅信息列表 */ private List<Map<String, Object>> getLibBorrowInfo(Spider spider) { try { String content = ""; content = spider.getGETContent(LibInfoPage); List<Map<String, Object>> borrowList = new ArrayList<Map<String, Object>>(); Pattern regex = Pattern.compile( "<table.*\\s*.*\\s*.*?<td>(?<title>.*?)<.*\\s*.*?td>(?<borrow>.*?)<.*\\s*.*?td>(?<back>.*?)<.*\\s*.*\\s*.*?td>(?<attach>.*?)<(?:.*\\s*){3}<form action=\"(?<href>[^\"]*)\""); Matcher regexMatcher = regex.matcher(content); while (regexMatcher.find()) { Map<String, Object> borrow = new HashMap<String, Object>(); borrow.put("bookName", regexMatcher.group("title")); borrow.put("borrowDate", regexMatcher.group("borrow")); borrow.put("expectDate", regexMatcher.group("back")); borrow.put("haveAttach", regexMatcher.group("attach")); borrow.put("renewHref", LibRenewActionBase + regexMatcher.group("href")); borrowList.add(borrow); } return borrowList; } catch (Exception e) { e.printStackTrace(); return null; } }
/** * 体育查询系统的登陆操作, 登录成功返回Spider, 登录失败返回null * * @param stuNum - 学生学号 * @param password - 密码 * @return 登录成功返回Spider, 登录失败返回null */ private Spider sportLogin(String stuNum, String password) { try { CloseableHttpClient client = HttpClients.createDefault(); Spider spider = new Spider(client); spider.getGETContent(SportLoginPage); List<NameValuePair> loginForm = new ArrayList<NameValuePair>(); loginForm.add(new BasicNameValuePair("username", stuNum)); loginForm.add(new BasicNameValuePair("password", password)); loginForm.add(new BasicNameValuePair("btnlogin.x", "31")); loginForm.add(new BasicNameValuePair("btnlogin.y", "6")); if (302 == spider.getPOSTCode(SportLoginAction, loginForm)) { return null; } return spider; } catch (Exception e) { e.printStackTrace(); return null; } }
/** * 借书查询的登录操作, 登录成功返回Spider, 登录失败返回null * * @param stuNum - 学生学号 * @param password - 密码 * @throws ClientProtocolException * @throws IOException * @return 登录成功返回Spider, 登录失败返回null */ private Spider libLogin(String stuNum, String password) { try { CloseableHttpClient client = HttpClients.createDefault(); Spider spider = new Spider(client); spider.getGETContent(LibHome); List<NameValuePair> loginForm = new ArrayList<NameValuePair>(); loginForm.add(new BasicNameValuePair("username", stuNum)); loginForm.add(new BasicNameValuePair("password", password)); loginForm.add(new BasicNameValuePair("backurl", "/uc/showPersonalSet.jspx")); loginForm.add(new BasicNameValuePair("schoolid", "482")); loginForm.add(new BasicNameValuePair("userType", "0")); if (302 != spider.getPOSTCode(LibLoginAction, loginForm)) { return null; } return spider; } catch (Exception e) { e.printStackTrace(); return null; } }
/** * 进行打卡信息抓取操作 * * @param spider - 已登录的Spider * @return 查询结果 */ private Map<String, Object> getSportInfo(Spider spider) { try { String content = ""; content = spider.getGETContent(SportInfoPage); System.out.println(content); Map<String, Object> sport = new HashMap<String, Object>(); String regTime = "有效次数:</td><td class='fd'>(.*?)</td>"; String regRun = "有效米数:</td><td class='fd'>(.*?)</td>"; String regRequire = "达标要求:</td><td class='fd'>(.*?)</td>"; Matcher matcherTime = Pattern.compile(regTime).matcher(content); Matcher matcherRun = Pattern.compile(regRun).matcher(content); Matcher matcherRequire = Pattern.compile(regRequire).matcher(content); sport.put("timeCount", matcherTime.find() ? matcherTime.group(1) : "0"); sport.put("runCount", matcherRun.find() ? matcherRun.group(1) : "0"); sport.put("require", matcherRequire.find() ? matcherRequire.group(1) : "0"); return sport; } catch (Exception e) { e.printStackTrace(); return null; } }