当前位置: 代码迷 >> Web前端 >> 搜寻记录数
  详细解决方案

搜寻记录数

热度:486   发布时间:2012-08-30 09:55:54.0
搜索记录数
/*
 * BaiduSeeker.java
 *
 * Created on 2008年2月29日, 下午4:03
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package zhangxin.demo;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

/**
 * 
 * @author 刘亚洲 qq:41832143
 */
public class BaiduSeeker {

	/** Creates a new instance of BaiduSeeker */
	public BaiduSeeker() {
	}

	public String getIndexPages(String url) {
		String beginurl = "http://www.baidu.com/s?wd=site%3A";
		String endurl = "&cl=3";
		String totalurl = beginurl + url + endurl;
		URL murl = null;
		URLConnection conn = null;
		try {
			murl = new URL(totalurl);
		} catch (MalformedURLException ex) {
			ex.printStackTrace();
		}
		try {
			conn = murl.openConnection();
			conn.setRequestProperty("User-Agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1)");
			conn.connect();
			InputStreamReader in = new InputStreamReader(conn.getInputStream());
			BufferedReader buff = new BufferedReader(in, in.toString().length());
			String index = "找到相关网页约";
			String index1 = "篇,用时";
			String ln;
			int begin;
			int end;
			while ((ln = buff.readLine()) != null) {
				if ((begin = ln.indexOf(index)) != -1) {
					System.out.println(ln);
					if ((end = ln.indexOf(index1)) != -1) {
//						System.out.println("begin" + begin);
//						System.out.println("end" + end);
//						System.out.println(ln.substring(begin + index.length(), end));
						buff.close();
						return ln.substring(begin + index.length(), end);
					}

				}
			}

		} catch (IOException ex) {
			ex.printStackTrace();
		}

		return null;
	}

	public static void main(String[] args) {
		BaiduSeeker b = new BaiduSeeker();
		System.out.println(b.getIndexPages("www.j2mehome.com"));
	}

}
?
  相关解决方案