码迷,mamicode.com
首页 > 编程语言 > 详细

Java HTML页面抓取实例

时间:2014-07-18 10:31:36      阅读:339      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   java   color   os   

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

public class Url {

    public static void main(String[] args) throws Exception{
        String html = getURLContent();
        System.out.println(html);
    }
    
    /**
     * 获取网页内容
     */
    private static String getURLContent() throws MalformedURLException, IOException, UnsupportedEncodingException {
        URL urlmy = new URL("http://www.baidu.com");

        HttpURLConnection con = (HttpURLConnection) urlmy.openConnection();
        HttpURLConnection.setFollowRedirects(true);
        con.setInstanceFollowRedirects(false);
        con.connect();

        BufferedReader br = new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));

        String s = "";

        StringBuffer sb = new StringBuffer();

        while ((s = br.readLine()) != null) {
            sb.append(s+"\r\n");
        }
        
        return sb.toString();
    }

}

Java HTML页面抓取实例,布布扣,bubuko.com

Java HTML页面抓取实例

标签:style   blog   http   java   color   os   

原文地址:http://www.cnblogs.com/shibazi/p/3852615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!