论坛首页 Java企业应用论坛

利用人人网去获取世界各地的大学信息

浏览 7241 次
精华帖 (0) :: 良好帖 (4) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2011-12-01  

最近登录校内在搜索一个大学老乡的时候,点击搜索时弹出一个框,这里边有世界各地的大学名称信息。于是便想要是能把这些数据给采集到,存起来,以后说不定能用到。于是有了下边的废话。 首先准备工具: 用到的jar包有: json-lib-2.4.jar(解析json数据用) json-lib-2.4.jar依赖的包有(commons-beanutils.jar,commons-collections-3.1.jar,commons-lang-2.3.jar,ezmorph-1.0.6.jar,morph-1.1.1.zip) 本来我想存到文件里的,后来想想还是存到数据库吧,于是又把以前常用到的工具类拿出来了 数据库用到的包有: commons-dbcp-1.4.jar commons-pool.jar mysql-connector-java-5.0.3-bin.jar 用到的抓包工具: httpwatchV7(强大的http分析工具) 其实在浏览器中做任何的动作,都是可以用JS脚本捕获到,并且进行处理的,那么点在搜索框中鼠标点下时,便会发出一个HTTP请求,服务器给出响应。我们关注的响应是在点击了搜索框之后通过http://s.xnimg.cn/a27085/allunivlist.js得到的响应,由于是在浏览器后台发出的,因此在浏览器我们并不能看到这个链接,可以通过httpwatch得到。分析响应内容是json格式的数据,并且是unicode编码的,因此我们可以用json这个包进行解析,转换称响应的对象,这样便能达到目地了。思路就是这么简单,不过关键是发现并去利用互联网强大的资源,如果你有更多的发现,利用自己所学的能够运用出去才是王道。下边我把我的主要的源代码程序传给大家,可以作一个参考,希望对你以后有所帮助. 

 

package demo;
//根式转换类
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;

public class EncoderUtils {
		public static String UnicodeToGB2312(String str) {
		String res = null;
		StringBuffer sb = new StringBuffer();
		try {
			while (str.length() > 0) {
				if (str.startsWith("\\u")) {
					int x = Integer.parseInt(str.substring(2, 6), 16);
					sb.append((char) x);
					str = str.substring(6);
				} else {
					sb.append(str.charAt(0));
					str = str.substring(1);
				}
			}
			res = sb.toString();
		} catch (Exception e) {
			e.printStackTrace(System.err);
		}
		return res;
	}
}

 package demo;

 

//文件读写工具类
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.StringReader;
import java.util.Properties;

public class StringUtils {
	// 将流写入一个文件
	public static void writeFileFromStream(String filename,InputStream in){
		if(filename==null || filename.trim().length()==0)
			return;
		File file=new File(filename);
    	if(!file.exists()){
    		try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
    	}
		FileOutputStream fou=null;
		try {
			fou = new FileOutputStream(file);
			byte []buffer=new byte[1024*4];
			int len=-1;
			while((len=in.read(buffer))!=-1){
				fou.write(buffer,0,len);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(in!=null)
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			if(fou!=null)
				try {
					fou.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
	}	
	//将字符串写进一个文件
	public static void writeFileFromString(String filename,String str){
		if(filename==null || filename.trim().length()==0)
			filename="tmp.txt";
    	File file=new File(filename);
    	if(!file.exists()){
    		try {
				file.createNewFile();
			} catch (IOException e) {
				e.printStackTrace();
			}
    	}
		BufferedWriter writer=null;
		BufferedReader reader=null;
		try {
			writer=new BufferedWriter(new FileWriter(file));
			reader=new BufferedReader(new StringReader(str));
			String tmp=null;
			StringBuffer buffer=new StringBuffer();
			while((tmp=reader.readLine())!=null)
				buffer.append(tmp+"\n");
			writer.write(buffer.toString());
			
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			try {
				reader.close();
				writer.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
    	
    }
	
	//从流中得到字符串格式内容
    public static String getStringFromStream(InputStream in) {
    	BufferedReader reader=null;
		reader = new BufferedReader(new InputStreamReader(in));
		 
		StringBuffer buffer=new StringBuffer();
		String str=null;
		try{
			while((str=reader.readLine())!=null){
				buffer.append(str+"\n");
			}	
			reader.close();
		}catch(Exception ex){
			ex.printStackTrace();
		}			
		return buffer.toString();
    }
    //得到数据库配置的属性信息
    public static Properties getDBconfig(){
    	Properties properties=new Properties();
    	InputStream in = null;
		try {
			in = new FileInputStream(new File("config/dbconfig.ini"));
			properties.load(in);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			if(in!=null)
				try {
					in.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
		}
		return properties;
    }																													

//由于代码较多,剩下的代码进行了打包。主类在ParserSchool
  • 大小: 27.5 KB
  • 大小: 68.4 KB
  • 大小: 35.3 KB
  • 大小: 64.7 KB
   发表时间:2011-12-02  
强大

我想获取所有股票信息

不知道哪里有接口

也想通过这种方式来搞
0 请登录后投票
   发表时间:2011-12-02  
yahoo.com上面有股票信息,每一分钟都会更新一次数据。。。
0 请登录后投票
   发表时间:2011-12-02  
下载的已经获取到了 没说如何获取?
0 请登录后投票
   发表时间:2011-12-02  
全世界的大学,中学信息真是无聊。
0 请登录后投票
   发表时间:2011-12-02  
不错的应用 方向!!
0 请登录后投票
   发表时间:2011-12-03  
值得学习!
0 请登录后投票
   发表时间:2011-12-05  
用rails来实现似乎更简洁一些。
0 请登录后投票
   发表时间:2011-12-05  
volking 写道
全世界的大学,中学信息真是无聊。

那你做一些有聊的事情来咯.
0 请登录后投票
   发表时间:2011-12-05  
呃,我曾经干过将体彩每一期的开奖结果都获取出来然后写入到数据库
0 请登录后投票
论坛首页 Java企业应用版

跳转论坛:
Global site tag (gtag.js) - Google Analytics