JAVA网页抓取

今天在看网页抓取,复习下JAVA,刚好没事情。

package web.Catch;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Test {
    public static void main(String[] args) throws Exception {
        String strURL = "http://ip.chinaz.com/?IP=127.0.0.1";  
        URL url = new URL(strURL);  
        HttpURLConnection httpConn = (HttpURLConnection) url.openConnection();  
        InputStreamReader input = new InputStreamReader(httpConn  
                .getInputStream(), "utf-8");  
        BufferedReader bufReader = new BufferedReader(input);  
        String line = "";  
        StringBuilder contentBuf = new StringBuilder();  
        while ((line = bufReader.readLine()) != null) {  
            contentBuf.append(line);  
        }  
        String buf = contentBuf.toString();  
        int beginIx = buf.indexOf(" ==>> ");  
        int endIx = buf.indexOf("</strong><br />");  
        String result = buf.substring(beginIx, endIx);  
        System.out.println("结果" + result);
    }
}

 

posted @ 2013-06-23 00:13  墨迹哥's  阅读(345)  评论(0编辑  收藏  举报