抓取毛概的题库 2018-06-21 我的小程序 java, 毛概 Comments 抓取毛概的题库123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.net.URL;import java.net.URLConnection;import javax.xml.parsers.DocumentBuilder;import javax.xml.parsers.DocumentBuilderFactory;import javax.xml.parsers.ParserConfigurationException;import org.w3c.dom.Document;import org.w3c.dom.Element;import org.xml.sax.SAXException;import com.google.gson.JsonArray;import com.google.gson.JsonObject;import com.google.gson.JsonParser;//导入马克思的题库,完成时间20118年6月21日晚上九点public class Demo { public static void main(String[] args) { String url = "http://211.67.48.87/wantsMWS/Question.asmx/GetQuestionList?CV=&QStyleCode=DD&QIDList="; //PD是判断题,DX是单选,DD是多选 int i = 1; while(i < 100000) { url += String.valueOf(i)+"&ScreenType=1"; test(url,i); i++; url = "http://211.67.48.87/wantsMWS/Question.asmx/GetQuestionList?CV=&QStyleCode=DD&QIDList="; } } static int aaaa=1; private static void test(String urls, int NO) { InputStream is = null; Document document = null; try { URL url = new URL(urls);//如"http://211.67.48.87/wantsMWS/Question.asmx/GetQuestionList?CV=&QStyleCode=DX&QIDList=2158&ScreenType=1" URLConnection con = url.openConnection(); is = con.getInputStream(); // DOM 解析器 DocumentBuilderFactory domfac = DocumentBuilderFactory.newInstance(); // DOM 工厂获得 DOM 解析器 DocumentBuilder dombuilder = domfac.newDocumentBuilder(); document = dombuilder.parse(is); } catch (SAXException e) { e.printStackTrace(); } catch (ParserConfigurationException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } // root要素取得 Element elementRoot = (document).getDocumentElement(); JsonParser parser=new JsonParser(); JsonObject object=(JsonObject)parser.parse(elementRoot.getTextContent().toString()); JsonArray QStyleList = object.get("List").getAsJsonArray(); //------------------------写文件 try { String path="C:\\Users\\Administrator\\Desktop\\毛泽东.txt"; File file=new File(path); if(!file.exists()) file.createNewFile(); FileOutputStream out=new FileOutputStream(file,true); //如果追加方式用true ,非追加用false for(int i = 0; i < QStyleList.size(); ++i) { JsonObject item = QStyleList.get(i).getAsJsonObject(); String SubjectCode = item.get("SubjectCode").getAsString(); //试卷类型 String QStyleCode = item.get("QStyleCode").getAsString(); //题目类型 String QID = item.get("QID").getAsString(); //题目ID String Topic = item.get("Topic").getAsString(); //题干 String IsRTF = item.get("IsRTF").getAsString(); //不知道是啥 JsonArray Option = item.get("OptionList").getAsJsonArray(); //写入文件 StringBuffer sb=new StringBuffer(); sb.append(aaaa+".题干:"+Topic+"\r\n"); System.out.println(SubjectCode); System.out.println("题干:"+Topic); for(int j = 0; j < Option.size(); ++j) { System.out.println("选项"+j+": "+Option.get(j).getAsString()); if(j==0)sb.append("A:"+Option.get(j).getAsString()+"\r\n"); if(j==1)sb.append("B:"+Option.get(j).getAsString()+"\r\n"); if(j==2)sb.append("C:"+Option.get(j).getAsString()+"\r\n"); if(j==3)sb.append("D:"+Option.get(j).getAsString()+"\r\n"); } String OptionMax = item.get("OptionMax").getAsString();//最大的选项 String OptionType = item.get("OptionType").getAsString(); //选项的类型 String Result = item.get("Result").getAsString(); //答案 System.out.println("答案为:"+Result); sb.append("答案:"+Result+"\r\n"); if("".equals(Topic)||Topic==null) { out.close(); System.exit(0); } if("MZT".equals(SubjectCode)) { aaaa++; out.write(sb.toString().getBytes("utf-8"));//注意需要转换对应的字符集 } } out.close(); } catch(IOException ex) { System.out.println(ex.getStackTrace()); } //----------------------------------写文件 } }