中间用到了jsoup解析html文档jsou的jar包可以在这里http://pan.baidu.com/s/1gd6AjnX下载。
另外主要利用的是httpClient模拟浏览器登录,只要是查询一个地方的信息httpclient不断开,就可以自动管理cookie,带着验证查询。
httpclient的jar包可以在这里http://pan.baidu.com/s/1pJCz7Vt下载。
1 package cn.liuning.UI; 2 3 import java.util.ArrayList; 4 import java.util.List; 5 import cn.liuning.find.FindCETScore; 6 import cn.liuning.find.FindLibrary; 7 import cn.liuning.javabean.Book; 8 import cn.liuning.javabean.Score; 9 10 11 public class Main { 12 13 /** 14 * @param args 15 * @throws Exception 16 */ 17 public static void main(String[] args) throws Exception { 18 19 //查询CET 20 //System.out.println("请输入您的准考证号:"); 21 //Scanner scanner = new Scanner(System.in); 22 //String examnumber = scanner.next(); 23 //System.out.println("请输入您的姓名:"); 24 //String name = scanner.next(); 25 FindCETScore findCet=new FindCETScore(); 26 List<Score> list1 = new ArrayList<Score>(); 27 list1 = findCet.findCETScore_Get("", ""); 28 findCet.output(list1); 29 30 //查询图书馆借书情况 31 FindLibrary findBook = new FindLibrary(); 32 //System.out.println("请输入您的账号:"); 33 //Scanner scanner = new Scanner(System.in); 34 //String account = scanner.next(); 35 //System.out.println("请输入您的密码:"); 36 //String password = scanner.next(); 37 List<Book> list2=findBook.findLibrary("",""); 38 findBook.output(list2); 39 40 } 41 42 } 43 -------------------------------------------------------------------------------- 44 package cn.liuning.find; 45 46 import java.util.ArrayList; 47 import java.util.List; 48 49 import org.apache.http.client.methods.CloseableHttpResponse; 50 import org.apache.http.client.methods.HttpGet; 51 import org.apache.http.impl.client.CloseableHttpClient; 52 import org.apache.http.impl.client.HttpClients; 53 import org.apache.http.util.EntityUtils; 54 55 import cn.liuning.Utils.CET_HtmlToJavaBean; 56 import cn.liuning.javabean.Score; 57 58 public class FindCETScore { 59 60 61 CloseableHttpClient httpclient= HttpClients.createDefault(); 62 List<Score> list = new ArrayList<Score>(); 63 64 //get方式获取 65 public List<Score> findCETScore_Get(String examnumber,String name) throws Exception { 66 try { 67 //371012141209811 刘宁 68 HttpGet httpGet1 = new HttpGet("http://www.chsi.com.cn/cet/query?zkzh=371012141209811&xm=刘宁"); 69 //设置请求头 70 httpGet1.setHeader("Referer", "http://www.chsi.com.cn/cet/");//这句话必须设置,有识别验证,不设置的话不知道是http://www.chsi.com.cn/cet/传过来的信息。 71 //执行 72 CloseableHttpResponse response3 = httpclient.execute(httpGet1); 73 try { 74 75 String str = EntityUtils.toString(response3.getEntity(),"utf-8"); 76 CET_HtmlToJavaBean thmlTobean = new CET_HtmlToJavaBean(); 77 list = thmlTobean.changetoBean(str); 78 return list; 79 80 } finally { 81 response3.close(); 82 } 83 } finally { 84 httpclient.close(); 85 } 86 } 87 88 //post方式获取 89 public String findCETScore_Post(CloseableHttpClient httpclient) throws Exception { 90 return null; 91 92 } 93 94 public void output(List<Score> list){ 95 Score score=new Score(); 96 score=(Score) list.get(0); 97 System.out.println("姓名:"+score.getName()); 98 System.out.println("学校:"+score.getSchool()); 99 System.out.println("考试类别:"+score.getCategory()); 100 System.out.println("准考证号:"+score.getExamnumber()); 101 System.out.println("考试时间:"+score.getTestTime()); 102 System.out.println("总分:"+score.getTotalScore()); 103 System.out.println("听力:"+score.getListen()); 104 System.out.println("阅读:"+score.getReading()); 105 System.out.println("写作与翻译:"+score.getTranslate()); 106 107 } 108 } 109 -------------------------------------------------------------------------------- 110 package cn.liuning.Utils; 111 import java.util.ArrayList; 112 import java.util.List; 113 114 import org.jsoup.Jsoup; 115 import org.jsoup.nodes.Document; 116 import org.jsoup.nodes.Element; 117 import org.jsoup.select.Elements; 118 import cn.liuning.javabean.Score; 119 120 public class CET_HtmlToJavaBean { 121 122 public List<Score> changetoBean(String str) 123 { 124 125 Document doc = Jsoup.parse(str); 126 Element table = doc.getElementsByTag("table").get(1); 127 Elements trs = table.getElementsByTag("tr"); 128 ArrayList<Score> list=new ArrayList<Score>(); 129 Score score = new Score(); 130 for (int i = 0; i < trs.size(); i++) 131 { 132 Element current_tr = trs.get(i); 133 Element td=current_tr.getElementsByTag("td").get(0); 134 String text=td.text(); 135 if(i==0){ 136 score.setName(text); 137 }else if(i==1){ 138 score.setSchool(text); 139 }else if(i==2){ 140 score.setCategory(text); 141 }else if(i==3){ 142 score.setExamnumber(text); 143 }else if(i==4){ 144 score.setTestTime(text); 145 }else if(i==5){ 146 score.setTotalScore(text.substring(0, 3)); 147 score.setListen(text.substring(8, 11)); 148 score.setReading(text.substring(16, 20)); 149 score.setTranslate(text.substring(27, 30)); 150 } 151 } 152 list.add(score); 153 return list; 154 } 155 } 156 157 --------------------------------------------------------------------------- 158 package cn.liuning.javabean; 159 160 public class Score { 161 private String name; 162 private String school; 163 private String category; 164 private String examnumber; 165 private String testTime; 166 private String totalScore; 167 private String listen; 168 private String reading; 169 private String translate; 170 171 public String getName() { 172 return name; 173 } 174 public void setName(String name) { 175 this.name = name; 176 } 177 public String getSchool() { 178 return school; 179 } 180 public void setSchool(String school) { 181 this.school = school; 182 } 183 public String getCategory() { 184 return category; 185 } 186 public void setCategory(String category) { 187 this.category = category; 188 } 189 public String getExamnumber() { 190 return examnumber; 191 } 192 public void setExamnumber(String examnumber) { 193 this.examnumber = examnumber; 194 } 195 public String getTestTime() { 196 return testTime; 197 } 198 public void setTestTime(String testTime) { 199 this.testTime = testTime; 200 } 201 public String getTotalScore() { 202 return totalScore; 203 } 204 public void setTotalScore(String totalScore) { 205 this.totalScore = totalScore; 206 } 207 public String getListen() { 208 return listen; 209 } 210 public void setListen(String listen) { 211 this.listen = listen; 212 } 213 public String getReading() { 214 return reading; 215 } 216 public void setReading(String reading) { 217 this.reading = reading; 218 } 219 public String getTranslate() { 220 return translate; 221 } 222 public void setTranslate(String translate) { 223 this.translate = translate; 224 } 225 }