爬取教务系统demo

package test;
import org.apache.commons.httpclient.*;
import org.apache.commons.httpclient.methods.*;
import org.apache.commons.httpclient.cookie.*;
import java.util.*;
import java.io.*;
/**
 *<p>Tile:LoginTest.java<p>
 *<p>Description:获取cookie和验证码模拟登录教务系统<p>
 * @author YXS<p>
 * @data2018年6月3日
 */
public class LoginTMSystem {

	public static String getHTML() {
		String cookie1 = "";
		String html = null;
		String txtSecretCode = "";
		/*
		 * System.out.println("请输入学号:");
		String txtUserName = new Scanner(System.in).next().trim();
		System.out.println("请输入密码:");
		String TextBox2 = new Scanner(System.in).next().trim();
		String RadioButtonList1 = "";
		String __VIEWSTATE = "";*/
		
		//登录url
		String loginURL = "http://jxfw.gdut.edu.cn/new/login";
		//教务管理系统首页url
		String indexURL = "http://jxfw.gdut.edu.cn/xsgrkbcx!xsgrkbMain.action";
		//登录后访问的课程表url
		String dataURL = "http://jxfw.gdut.edu.cn/xsgrkbcx!getKbRq.action?xnxqdm=201802&zc=7";
		//验证码下载网址
		String checkCodeURL = "http://jxfw.gdut.edu.cn/yzm?d="+new Date().getTime();
		//创建浏览器对象
		HttpClient httpClient = new HttpClient();
				
		//先访问验证码页面,获取验证码
		GetMethod getMethod1 = new GetMethod(checkCodeURL);
		try {
			//设置HttpClient接收Cookie
			httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
			httpClient.executeMethod(getMethod1);
			//获取访问后的Cookie
			Cookie[] cookies1 = httpClient.getState().getCookies();
			StringBuffer tmpcookies1 = new StringBuffer();
			for(Cookie c1 : cookies1) {
				tmpcookies1.append(c1.toString()+";");
				System.out.println("访问页面cookies : "+c1.toString());
				cookie1 = tmpcookies1.toString();
				//cookie1 = c1.toString();
			}
			
			//验证码保存路径
			File storeFile = new File("C:\\Users\\Administrator\\Desktop\\ver.png");
			InputStream is = getMethod1.getResponseBodyAsStream();
			FileOutputStream fos = new FileOutputStream(storeFile);
			byte[] b = new byte[1024];
			int n;
			while((n = is.read(b)) != -1) {
				fos.write(b,0,n);
			}
			is.close();
			fos.close();
		}catch(Exception e) {
			e.printStackTrace();
		}
		//System.out.println("请输入验证码:");
		//String txtSecretCode = new Scanner(System.in).next().trim();
	
		System.out.println("测试使用cookie1:"+cookie1);
		
		//模拟登录,按实际服务器端要求选用Post 或 Get请求方式
		PostMethod postMethod = new PostMethod(loginURL);
		//设置相同的cookie,重要!!!!!!
		postMethod.setRequestHeader("cookie",cookie1);
		//设置登录时需要的信息,用户名和密码
		NameValuePair[] postData=new NameValuePair[3];
		postData[0]=new NameValuePair("account","3117002575");
		postData[1]=new NameValuePair("pwd","xxxxxxxxxxx");
		Scanner in=new Scanner(System.in);
		String verifycode=in.nextLine();
		postData[2]=new NameValuePair("verifycode",verifycode);
		postMethod.setRequestBody(postData);
		try {
			int statusCode = httpClient.executeMethod(postMethod);
			//html = postMethod.getResponseBodyAsString();
			System.out.println(statusCode);
			InputStream input=postMethod.getResponseBodyAsStream();
			String filename="C:\\Users\\Administrator\\Desktop\\page.txt";
			OutputStream out=new FileOutputStream(filename);
			int tempByte=-1;
			while ((tempByte=input.read())>0) {
				out.write(tempByte);
			}
			File file=new File(filename);
			InputStream inputStream=new FileInputStream(file);
			InputStreamReader reader=new InputStreamReader(inputStream,"utf-8");
			BufferedReader brf=new BufferedReader(reader);
			String str=brf.readLine();
			System.out.println(str);
			str=str.substring(str.indexOf("/"), str.length());
			System.out.println(str.substring(0,str.indexOf("\"")));
			brf.close();
			//重定向
			if(statusCode == 200) { 
				System.out.println("模拟登录成功!");
				
				//设置HttpClient接收Cookie,用与浏览器一样的策略
				httpClient.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
				//获取登录后的Cookie
				Cookie[] cookies = httpClient.getState().getCookies();
				StringBuffer tmpcookies = new StringBuffer();
				for(Cookie c : cookies) {
					tmpcookies.append(c.toString()+";");
					System.out.println("登录页面cookies : "+c.toString());
				}
				//System.out.println("-------------------------------InformationFrame------------------------------");
				/*
				 * //进行登录后的操作
				
				//访问主页面
				GetMethod getMethod2 = new GetMethod(indexURL);
				//每次访问需授权的网址时带上前面的cookie作为通行证
				getMethod2.setRequestHeader("cookie",tmpcookies.toString());
				httpClient.executeMethod(getMethod2);
				*/
				//访问课程表页面
				GetMethod getMethod3 = new GetMethod(dataURL);
				//每次访问需授权的网址时带上前面的cookie作为通行证
				
				getMethod3.setRequestHeader("cookie",tmpcookies.toString());
				httpClient.executeMethod(getMethod3);
				html = getMethod3.getResponseBodyAsString();
				/*
				//访问主页面
				GetMethod getMethod = new GetMethod(indexURL);
				//每次访问需授权的网址时带上前面的cookie作为通行证
				getMethod.setRequestHeader("cookie",tmpcookies.toString());
				httpClient.executeMethod(getMethod);
				html = getMethod.getResponseBodyAsStream();
				*/
			}else {
				System.out.println("登录失败");
			}
		}catch(Exception e) {
			e.printStackTrace();
		}
		return html;
	}
	public static void main(String[] args) throws IOException {
		String str=getHTML();
		String temp=null;
		List<String> list=new ArrayList<String>();
		/*
		
		
		
		
		*/
		str=str.replaceAll("\\,", "");
		str=str.replaceAll("\\]", "ff");
		str=str.replaceAll("\\[", "gg");
		str=str.substring(5,str.indexOf("ffgg"));
		str=str.replaceAll("\\{", "ge");
		str=str.replaceAll("\\}", "fen");
		while (str!=null) {
			temp=str.substring(0,str.indexOf("fen"));
			list.add(temp);
			if (str.indexOf("fenge")>0) {
				str=str.substring(str.indexOf("fenge")+5);
			} else {
				str=null;
			}
		}
		System.out.println("加载完成!!");
		Scanner input=new Scanner(System.in);
		while (true) {
			String in=input.nextLine();
			if (in.equals("#")) {
				break;
			} else {
				Iterator<String> it=list.iterator();
				String str3="找不到此课程";
				while (it.hasNext()) {
					String str2=it.next();
					if (str2.indexOf(in)!=-1) {
						System.out.println(str2);
						str3="";
						break;
					} 
				}
				System.out.println(str3);
			}
		}
		/*
		File file=new File("C:\\Users\\Administrator\\Desktop\\get.html");
		try {
			OutputStream out=new FileOutputStream(file);
			InputStream in=getHTML();
			byte[] bs=new byte[1024];
			int n;
			while ((n=in.read(bs))>0) {
				out.write(bs, 0, n);
			}
			System.out.println("ok");
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		*/
	}

}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值