zoukankan      html  css  js  c++  java
  • Java HTML页面抓取实例

    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+"
    ");
            }
            
            return sb.toString();
        }
    
    }
  • 相关阅读:
    全球化编码
    linuxGrep命令
    Xcode-插件所在路径
    多控制器间数据传递
    触摸事件
    IOS事件处理
    事件监听的三种方法
    UITabBarController
    聊天布局
    Info.plist:项目配置文件
  • 原文地址:https://www.cnblogs.com/shibazi/p/3852615.html
Copyright © 2011-2022 走看看