Java实现简单网页抓取

需求说明:使用Java抓取网页信息,并以字符串的形式返回。

使用Java代码实现:

package net.ibuluo.spider.util;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.net.MalformedURLException;
import java.net.URL;

/**
 * Http工具
 * @author robin zhang
 *
 */
public class HttpUtil {
    
    /**
     * 根据网址抓取网页信息并将之以字符串的形式返回
     * @param urlStr
     *             网址字符串
     * @return
     * @throws MalformedURLException 
     */
    public static String getUrl(String urlStr) {
        
        String result = null;
        
        try{
            URL url = new URL(urlStr);
            result = inputStream2String(url.openStream());
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        
        return result;
    }
    
    
    /**
     * 读取字节流中的信息,并转为字符串
     * @param inputStream
     *             要读取的字节流
     * @return
     * @throws IOException
     */
    private static String inputStream2String(InputStream inputStream) 
                        throws IOException{
        Reader reader = null;
        StringBuilder builder = null;
        try{
            //将字节流转为字符流
            reader = new InputStreamReader(inputStream);
            //创建字符串容器
            builder = new StringBuilder();
            //设置字符流读取长度
            char[] buffer = new char[1024];
            //记录每次读取的长度,主要是为记录最后一次读取的长度
            int offset = 0;
            while((offset=reader.read(buffer)) > 0){
                //将读取的内容转为字符串并放入builder中
                builder.append( new String(buffer, 0, offset) );
            }
            return builder.toString();
        } catch (IOException e) {
            e.printStackTrace();
        }finally{
            if(null != reader){
                reader.close();
            }
        }
        return null;
    }
    
    
    public static void main(String[] args){
        System.out.println( getUrl("http://www.ibuluo.net/") );
    }
}

以上的内容可以使用一个第三方插件JSoup实现。使用jsoup实现代码如下:

    try {
            Document doc = Jsoup.connect("http://www.baidu.com/").get();
            System.out.println(doc.html());
        } catch (IOException e) {
            e.printStackTrace();
        }

Jsoup更多的用处是在实现html文档的分析上。可以参考Jsoup官方网站

posted @ 2014-11-07 22:28  robin·张  阅读(623)  评论(0编辑  收藏  举报