Java模拟登录正方教务抓取成绩、课表、空教室
程序员文章站
2024-03-09 12:31:23
本文实例为大家分享了java模拟登录正方教务抓取成绩、课表、空教室等信息,供大家参考,具体内容如下
1.jwgl.java
package com.ican...
本文实例为大家分享了java模拟登录正方教务抓取成绩、课表、空教室等信息,供大家参考,具体内容如下
1.jwgl.java
package com.ican.yueban.jwgl; import java.io.ioexception; import java.io.inputstream; import java.util.arraylist; import java.util.list; import java.util.scanner; import org.apache.http.httpresponse; import org.apache.http.namevaluepair; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.entity.urlencodedformentity; import org.apache.http.client.methods.closeablehttpresponse; import org.apache.http.client.methods.httpget; import org.apache.http.client.methods.httppost; import org.apache.http.impl.client.closeablehttpclient; import org.apache.http.impl.client.httpclients; import org.apache.http.message.basicnamevaluepair; import org.jsoup.jsoup; import org.jsoup.nodes.document; import org.jsoup.select.elements; import com.ican.yueban.utils.dateutils; import com.ican.yueban.utils.globalconstant; import com.ican.yueban.utils.ioutils; import com.ican.yueban.utils.parseutils; public class jwgl { private static string stunumber = ""; private static string stuname = ""; private static string cookie = ""; private string indexurl = globalconstant.index_url; private string secretcodeurl = globalconstant.secretcode_url; private string loginurl = globalconstant.login_url; private string mainurl = globalconstant.main_url; private string queryclassroomurl = globalconstant.query_classroom_url; private string queryclassroomgnmkdm = globalconstant.query_classroom_gnmkdm; private string querystugradeurl = globalconstant.query_stu_grade_url; private string querystugradegnmkd = globalconstant.query_stu_grade_gnmkdm; private string querystucourseurl = globalconstant.query_stu_course_url; private string querystucoursegnmkd = globalconstant.query_stu_course_gnmkdm; private string xixiaoqu = globalconstant.xixiaoqu; private string identitystu = globalconstant.identity_stu; /** * 登录功能 * * @param stunumber * @param password * @return * @throws exception * @throws unsupportedoperationexception */ public boolean login(string stunumber, string password) throws unsupportedoperationexception, exception { this.stunumber = stunumber; // 获取验证码 httpget secretcodeget = new httpget(secretcodeurl); closeablehttpclient client = httpclients.createdefault(); closeablehttpresponse responsesecret = client.execute(secretcodeget); // 获取返回的cookie cookie = responsesecret.getfirstheader("set-cookie").getvalue(); string viewstate = ioutils.getviewstate(indexurl, "", ""); // 将验证码下载到c盘 ioutils.getsecret(responsesecret.getentity().getcontent(), "secretcode.png", "c://"); scanner sc = new scanner(system.in); system.out.println("请输入验证码:"); // 手动填充刚才获取的验证码的值 string secret = sc.next().trim(); httppost loginpost = new httppost(loginurl);// 创建登录的post请求 loginpost.setheader("cookie", cookie);// 带上第一次请求的cookie list<namevaluepair> namevaluepairlogin = new arraylist<namevaluepair>();// 封装post提交参数 namevaluepairlogin .add(new basicnamevaluepair("__viewstate", viewstate));// 隐藏表单值 namevaluepairlogin .add(new basicnamevaluepair("txtusername", stunumber));// 学号 namevaluepairlogin.add(new basicnamevaluepair("textbox2", password));// 密码 namevaluepairlogin.add(new basicnamevaluepair("txtsecretcode", secret));// 验证码 namevaluepairlogin.add(new basicnamevaluepair("radiobuttonlist1", identitystu));// 身份,默认学生 namevaluepairlogin.add(new basicnamevaluepair("button1", "")); namevaluepairlogin.add(new basicnamevaluepair("lblanguage", "")); namevaluepairlogin.add(new basicnamevaluepair("hidpdrs", "")); namevaluepairlogin.add(new basicnamevaluepair("hidsc", "")); urlencodedformentity entity = new urlencodedformentity( namevaluepairlogin, "gb2312"); loginpost.setentity(entity); httpresponse responselogin = client.execute(loginpost); // client1.close(); // 第三步:判断提交数据是否成功,成功返回302 if (responselogin.getstatusline().getstatuscode() == 302) { // 如果提交成功,带着cookie请求重定向的main页面,并获取学生姓名 httpget mainget = new httpget(mainurl + stunumber); mainget.setheader("cookie", cookie); mainget.setheader("referer", loginurl); httpresponse responsemain = client.execute(mainget); inputstream is = responsemain.getentity().getcontent(); string html = ""; try { html = ioutils.gethtml(is, "gb2312"); } catch (exception e) { system.out.println("解析html失败!"); e.printstacktrace(); } stuname = jsoup.parse(html).getelementbyid("xhxm").text(); system.out.println("登录成功!欢迎您:" + stuname); client.close(); return true; } else { system.out.println("登录失败!"); client.close(); return false; } } /** * 查询空教室 * * @throws exception * * @throws exception */ public void queryclassroom(string xiaoqu, string xqj, string sjd) throws exception { closeablehttpclient client = httpclients.createdefault(); string newqueryclassrommurl = queryclassroomurl + stunumber + "&xm=" + stuname + queryclassroomgnmkdm;// 拼接请求的url string parsesjd = parseutils.parseweek(sjd);// 解析当前节次对应的字符串 string nowweek = dateutils.getweek() + "";// 获取当前时间是第几周 string viewstate = ioutils.getviewstate(newqueryclassrommurl, cookie, mainurl + stunumber); // 封装查询空教室请求参数 list<namevaluepair> queryclassroompair = new arraylist<namevaluepair>(); queryclassroompair.add(new basicnamevaluepair("__eventtarget", "")); queryclassroompair.add(new basicnamevaluepair("__eventargument", "")); queryclassroompair .add(new basicnamevaluepair("__viewstate", viewstate)); queryclassroompair.add(new basicnamevaluepair("xiaoq", xiaoqu));// 校区类型,默认西校区 queryclassroompair.add(new basicnamevaluepair("jslb", ""));// 教室类别,默认为空 queryclassroompair.add(new basicnamevaluepair("min_zws", "0"));// 最小座位数,默认为0 queryclassroompair.add(new basicnamevaluepair("max_zws", ""));// 最大座位数,默认为空 queryclassroompair.add(new basicnamevaluepair("ddlksz", nowweek));// 起始周,默认当前周 queryclassroompair.add(new basicnamevaluepair("ddljsz", nowweek));// 结束周,默认当前周 queryclassroompair.add(new basicnamevaluepair("xqj", xqj));// 星期几,默认当天 queryclassroompair.add(new basicnamevaluepair("ddldsz", ""));// 单双周,默认 queryclassroompair.add(new basicnamevaluepair("sjd", parsesjd));// 第几节 queryclassroompair.add(new basicnamevaluepair("button2", "空教室查询")); queryclassroompair.add(new basicnamevaluepair("xn", "2015-2016")); queryclassroompair.add(new basicnamevaluepair("xq", "2")); queryclassroompair.add(new basicnamevaluepair("ddlsyxn", "2015-2016")); queryclassroompair.add(new basicnamevaluepair("ddlsyxq", "2")); urlencodedformentity entityclassroom = new urlencodedformentity( queryclassroompair); httppost queryclassroompost = new httppost(newqueryclassrommurl); // newqueryclassrommurl示例:http://jwgl2.ujn.edu.cn/xxjsjy.aspx?xh=20121214104&xm=xxx&gnmkdm=n121611 queryclassroompost.setentity(entityclassroom); queryclassroompost.setheader("referer", mainurl + stunumber);// 设置头信息 queryclassroompost.setheader("cookie", cookie); httpresponse responseclassroom = client.execute(queryclassroompost); inputstream is = responseclassroom.getentity().getcontent(); string html = ioutils.gethtml(is, "gb2312"); document doc = jsoup.parse(html); elements eleclassroom = doc.select("td"); elements eleinfo = doc.select("#lblbt"); system.out.println(eleinfo.get(0).text()); for (int i = 0; i < eleclassroom.size(); i++) { // 只打印教室名称 if (i % 8 == 1) { system.out.println(eleclassroom.get(i).text()); } } client.close(); } /** * 重载查询空教室方法,默认时间,课程节次的无参数查询方法 * * @throws ioexception * @throws clientprotocolexception */ public void queryclassroom() throws clientprotocolexception, ioexception, exception { string weekday = dateutils.getweekday() + "";// 获取当前时间是星期几 string sdj = dateutils.getnowcourse() + "";// 获取当前时间是第几节课 new jwgl().queryclassroom(xixiaoqu, weekday, sdj); } /** * 查询个人成绩方法 * * @throws clientprotocolexception * @throws ioexception */ public void querystugrade(string xn, string xq) throws clientprotocolexception, ioexception { closeablehttpclient client = httpclients.createdefault(); string newquerystugradeurl = querystugradeurl + stunumber + "&xm=" + stuname + querystugradegnmkd; httppost querygradepost = new httppost(newquerystugradeurl); string viewstate = ioutils.getviewstate(newquerystugradeurl, cookie, mainurl + stunumber); // 封装请求参数 list<namevaluepair> querygradepair = new arraylist<namevaluepair>(); querygradepair.add(new basicnamevaluepair("__eventtarget", "")); querygradepair.add(new basicnamevaluepair("__eventargument", "")); querygradepair.add(new basicnamevaluepair("__viewstate", viewstate)); querygradepair.add(new basicnamevaluepair("hidlanguage", "")); querygradepair.add(new basicnamevaluepair("ddlxn", xn));// 学年 querygradepair.add(new basicnamevaluepair("ddlxq", xq));// 学期 querygradepair.add(new basicnamevaluepair("ddl_kcxz", "")); querygradepair.add(new basicnamevaluepair("btn_xq", "学期成绩")); querygradepost.setheader("cookie", cookie); querygradepost.setheader("referer", mainurl + stunumber); urlencodedformentity entitygrade = new urlencodedformentity( querygradepair); querygradepost.setentity(entitygrade); httpresponse responquerygradepost = client.execute(querygradepost); string gradehtml = ioutils.gethtml(responquerygradepost.getentity() .getcontent(), "gb2312"); // system.out.println(gradehtml); document gradedoc = jsoup.parse(gradehtml); elements elegrade = gradedoc.select("td"); // 按需求解析html<td>标签内容并输出 for (int i = 0; i < 7; i++) { system.out.println(elegrade.get(i).text()); } for (int i = 11; i < elegrade.size(); i = i + 10) { if (i + 15 < elegrade.size()) { system.out.print(elegrade.get(i).text() + " "); i = i + 5; system.out.print(elegrade.get(i).text()); system.out.println(); } client.close(); } } /** * 查询个人课表方法 * * @param xnd * @param xqd * @throws clientprotocolexception * @throws ioexception */ public void querystucourse(string xnd, string xqd) throws clientprotocolexception, ioexception { closeablehttpclient client = httpclients.createdefault(); string newquerystucourseurl = querystucourseurl + stunumber + "&xm=" + stuname + querystucoursegnmkd; string viewstate = ioutils.getviewstate(newquerystucourseurl, cookie, mainurl + stunumber); httppost querystucoursepost = new httppost(newquerystucourseurl); list<namevaluepair> stucoursepair = new arraylist<namevaluepair>(); stucoursepair.add(new basicnamevaluepair("__eventtarget", "xqd")); stucoursepair.add(new basicnamevaluepair("__eventargument", "")); stucoursepair.add(new basicnamevaluepair("__viewstate", viewstate)); stucoursepair.add(new basicnamevaluepair("xnd", xnd)); stucoursepair.add(new basicnamevaluepair("xqd", xqd)); urlencodedformentity entitysource = new urlencodedformentity( stucoursepair); querystucoursepost.setentity(entitysource); querystucoursepost.setheader("cookie", cookie); querystucoursepost.setheader("referer", mainurl + stunumber); httpresponse responsestucourse = client.execute(querystucoursepost); string html = ioutils.gethtml(responsestucourse.getentity() .getcontent(), "gb2312"); document doccourse = jsoup.parse(html); elements elecourse = doccourse.select("td"); for (int i = 2; i < elecourse.size(); i++) { system.out.print(elecourse.get(i).text() + " "); if (i % 9 == 0) { system.out.println(); } } } public static void main(string[] args) { jwgl jw = new jwgl(); try { jw.login("这里是学号", "这里是密码"); system.out.println("查询成绩测试-------"); jw.querystugrade("2015-2016", "1"); // 查询西校区,周一,第12节空教室测试。 // jw.queryclassroom("1", "1", "2"); system.out.println("查询空教室测试------"); jw.queryclassroom(); system.out.println("查询个人课表测试-------"); jw.querystucourse("2014-2015", "1"); } catch (clientprotocolexception e) { e.printstacktrace(); } catch (ioexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } //qq:451209214 } }
2.dateutils.java
package com.ican.yueban.utils; import java.text.parseexception; import java.text.simpledateformat; import java.util.calendar; import java.util.date; public class dateutils { private static string startday = globalconstant.start_day;// 开学日期 private static string endday = globalconstant.end_day;// 放假日期 /** * 获取当前时间是第几节课,只限8-16点之间,其他时间默认1,2节课。 * * @return */ public static int getnowcourse() { simpledateformat df = new simpledateformat("hh:mm:ss");// 设置日期格式 string nowdate = df.format(new date()); if (nowdate.startswith("08") || nowdate.startswith("09")) { return 1;// 12节课。 } else if (nowdate.startswith("10") || nowdate.startswith("11")) { return 2;// 34节课,以此类推。 } else if (nowdate.startswith("12") || nowdate.startswith("13") || nowdate.startswith("14")) { return 3; } else if (nowdate.startswith("15") || nowdate.startswith("16")) { return 4; } else { return 1; } } /** * 获取当前时间是第几周 * * @return */ public static int getweek() { int days = 0; int nowweek = 0; try { simpledateformat df = new simpledateformat("yyyy-mm-dd");// 设置日期格式 string nowdate = df.format(new date()); int nowdaysbetween = daysbetween(startday, nowdate) + 1; days = daysbetween(startday, endday); int x = nowdaysbetween % 7; if (x == 0) { nowweek = nowdaysbetween / 7; } else { nowweek = nowdaysbetween / 7 + 1; } } catch (parseexception e) { system.out.println("输入的日期不合法,解析日期失败"); e.printstacktrace(); } return nowweek; } /** * 获取当前时间是星期几 * * @return */ public static int getweekday() { calendar cal = calendar.getinstance(); cal.settime(new date()); if (cal.get(calendar.day_of_week) - 1 == 0) { return 7; } return cal.get(calendar.day_of_week) - 1; } /** * 计算两个string类型日期之间的天数 * * @param startday * @param endday * @return * @throws parseexception */ public static int daysbetween(string startday, string endday) throws parseexception { simpledateformat sdf = new simpledateformat("yyyy-mm-dd"); calendar cal = calendar.getinstance(); cal.settime(sdf.parse(startday)); long time1 = cal.gettimeinmillis(); cal.settime(sdf.parse(endday)); long time2 = cal.gettimeinmillis(); long between_days = (time2 - time1) / (1000 * 3600 * 24); return integer.parseint(string.valueof(between_days)); } /** * 以yyyy-mm-dd hh:mm:ss格式返回string类型系统时间 * * @return */ public static string getnowdate() { simpledateformat df = new simpledateformat("yyyy-mm-dd hh:mm:ss");// 设置日期格式 return df.format(new date()); } }
3.globalconstant.java
package com.ican.yueban.utils; /** * 本系统所有常量定义 * * @author 宋开宗 * */ public interface globalconstant { public static final string start_day = "2016-02-29"; public static final string end_day = "2016-07-10"; public static final string index_url = "http://jwgl2.ujn.edu.cn";// 济大教务系统首页 public static final string secretcode_url = "http://jwgl2.ujn.edu.cn/checkcode.aspx";// 验证码页面 public static final string login_url = "http://jwgl2.ujn.edu.cn/default2.aspx";// 济大教务系统登录页 public static final string main_url = "http://jwgl2.ujn.edu.cn/xs_main.aspx?xh=";// 济大教务系统主页,菜单页面 public static final string query_classroom_url = "http://jwgl2.ujn.edu.cn/xxjsjy.aspx?xh=";// 济大查询空教室链接 public static final string query_classroom_gnmkdm = "&gnmkdm=n121611";// 济大查询空教室gnmkdm public static final string query_stu_course_url = "http://jwgl2.ujn.edu.cn/xskbcx.aspx?xh=";// 济大查询个人课表链接 public static final string query_stu_course_gnmkdm = "&gnmkdm=n121603";// 济大查询个人课表gnmkdm public static final string query_stu_grade_url = "http://jwgl2.ujn.edu.cn/xscjcx.aspx?xh=";// 济大查询个人成绩链接 public static final string query_stu_grade_gnmkdm = "&gnmkdm=n121605";// 济大查询个人成绩gnmkdm public static final string identity_stu = "学生";// 身份:学生 public static final string xixiaoqu = "1";// 济大西校区标志 public static final string dongxiaoqu = "2";// 济大东校区标志 public static final string zhangqiuxiaoqu = "3";// 济大章丘校区标志 public static final string class1 = "'1'|'1','0','0','0','0','0','0','0','0'";// 1,2节 public static final string class2 = "'2'|'0','3','0','0','0','0','0','0','0'";// 3,4节 public static final string class3 = "'3'|'0','0','5','0','0','0','0','0','0'";// 5,6节 public static final string class4 = "'4'|'0','0','0','7','0','0','0','0','0'";// 7,8节 public static final string class5 = "'5'|'0','0','0','0','9','0','0','0','0'";// 8,10节 public static final string class6 = "'6'|'0','0','0','0','0','11','0','0','0'";// 11,12节 public static final string class7 = "'7'|'1','3','0','0','0','0','0','0','0'";// 上午 public static final string class8 = "'8'|'0','0','5','7','0','0','0','0','0'";// 下午 public static final string class9 = "'9'|'1','3','5','7','0','0','0','0','0'";// 白天 public static final string class10 = "'10'|'0','0','0','0','9','11','0','0','0'";// 晚上 public static final string class11 = "'11'|'1','3','5','7','9','11','0','0','0'";// 全天 public static final string btn_xueqi = "btn_xq";// 学期成绩 public static final string btn_xuenian = "btn_xn";// 学年成绩 public static final string btn_linian = "btn_zcj";// 历年成绩 public static final string university_code_ujn = "00001";// 济南大学标识代码 public static final string user_state_n = "1";// 未认证 public static final string user_state_y = "2";// 认证通过 public static final string comment_type_newthings = "1";// 评论类型1:新鲜事 public static final string comment_type_interest = "2";// 评论类型2:兴趣活动 }
4.ioutils.java
package com.ican.yueban.utils; import java.awt.rectangle; import java.awt.image.bufferedimage; import java.io.bytearrayoutputstream; import java.io.file; import java.io.fileinputstream; import java.io.fileoutputstream; import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.net.url; import java.net.urlconnection; import java.util.iterator; import javax.imageio.imageio; import javax.imageio.imagereadparam; import javax.imageio.imagereader; import javax.imageio.stream.imageinputstream; import org.apache.http.client.clientprotocolexception; import org.apache.http.client.methods.httpget; import org.apache.http.impl.client.closeablehttpclient; import org.apache.http.impl.client.httpclients; import org.jsoup.jsoup; public class ioutils { /** * 指定编码格式 ,把输入流转化为字符串 * * @param is * @return * @throws ioexception */ public static string gethtml(inputstream is, string encoding) throws ioexception { bytearrayoutputstream bos = new bytearrayoutputstream(); byte[] buffer = new byte[1024]; int len = 0; while ((len = is.read(buffer)) != -1) { bos.write(buffer, 0, len); } is.close(); return new string(bos.tobytearray(), encoding); } /** * 下载图片 * * @param urlstring * @param filename * @param savepath * @throws exception */ public static void download(string urlstring, string filename, string savepath) throws exception { // 构造url url url = new url(urlstring); // 打开连接 urlconnection con = url.openconnection(); // 设置请求超时为5s con.setconnecttimeout(5 * 1000); // 输入流 inputstream is = con.getinputstream(); // 1k的数据缓冲 byte[] bs = new byte[1024]; // 读取到的数据长度 int len; // 输出的文件流 file sf = new file(savepath); if (!sf.exists()) { sf.mkdirs(); } outputstream os = new fileoutputstream(sf.getpath() + "\\" + filename); // 开始读取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } // 完毕,关闭所有链接 os.close(); is.close(); } /** * 图片裁剪工具类 * * @param src * @param dest * @param x * @param y * @param w * @param h * @throws ioexception */ public static void cutimage(string src, string dest, int x, int y, int w, int h) throws ioexception { iterator iterator = imageio.getimagereadersbyformatname("jpg"); imagereader reader = (imagereader) iterator.next(); inputstream in = new fileinputstream(src); imageinputstream iis = imageio.createimageinputstream(in); reader.setinput(iis, true); imagereadparam param = reader.getdefaultreadparam(); rectangle rect = new rectangle(x, y, w, h); param.setsourceregion(rect); bufferedimage bi = reader.read(0, param); imageio.write(bi, "jpg", new file(dest)); in.close(); } /** * 判断字符编码集 * * @param str * @return */ public static string getencoding(string str) { string encode = "gb2312"; try { if (str.equals(new string(str.getbytes(encode), encode))) { string s = encode; return s; } } catch (exception exception) { } encode = "iso-8859-1"; try { if (str.equals(new string(str.getbytes(encode), encode))) { string s1 = encode; return s1; } } catch (exception exception1) { } encode = "utf-8"; try { if (str.equals(new string(str.getbytes(encode), encode))) { string s2 = encode; return s2; } } catch (exception exception2) { } encode = "gbk"; try { if (str.equals(new string(str.getbytes(encode), encode))) { string s3 = encode; return s3; } } catch (exception exception3) { } return "未知"; } /** * 把输入流转换成图片---》获取验证码 * * @param is * @param filename * @param savepath * @throws exception */ public static void getsecret(inputstream is, string filename, string savepath) throws exception { // 1k的数据缓冲 byte[] bs = new byte[1024]; // 读取到的数据长度 int len; // 输出的文件流 file sf = new file(savepath); if (!sf.exists()) { sf.mkdirs(); } outputstream os = new fileoutputstream(sf.getpath() + "\\" + filename); // 开始读取 while ((len = is.read(bs)) != -1) { os.write(bs, 0, len); } // 完毕,关闭所有链接 os.close(); is.close(); } /** * 获取隐藏字段的__viewstate值 * * @param url * @param cookie * @param referer * @return * @throws unsupportedoperationexception * @throws clientprotocolexception * @throws ioexception */ public static string getviewstate(string url, string cookie, string referer) throws unsupportedoperationexception, clientprotocolexception, ioexception { closeablehttpclient client = httpclients.createdefault(); httpget getviewstate = new httpget(url); getviewstate.setheader("cookie", cookie); getviewstate.setheader("referer", referer);// 设置头信息 string s = ioutils.gethtml(client.execute(getviewstate).getentity() .getcontent(), "gb2312"); string viewstate = jsoup.parse(s).select("input[name=__viewstate]") .val(); client.close(); return viewstate; } }
5. parseutils.java
package com.ican.yueban.utils; public class parseutils { /** * 获取课程节次对应的字符串 * @param course * @return */ public static string parseweek(string course) { string sjd=""; int nowcourse = integer.parseint(course); switch (nowcourse) { case 1: sjd=globalconstant.class1; break; case 2: sjd=globalconstant.class2; break; case 3: sjd=globalconstant.class3; break; case 4: sjd=globalconstant.class4; break; case 5: sjd=globalconstant.class5; break; case 6: sjd=globalconstant.class6; break; case 7: sjd=globalconstant.class7; break; case 8: sjd=globalconstant.class8; break; case 9: sjd=globalconstant.class9; break; case 10: sjd=globalconstant.class10; break; case 11: sjd=globalconstant.class11; break; default: sjd=globalconstant.class1; break; } return sjd; } }
以上就是本文的全部内容,希望对大家的学习有所帮助。