zoukankan      html  css  js  c++  java
  • java 获取网页指定内容

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    public class HttpTest {
     String urlString;
     public static void main(String[] args) throws Exception {
      HttpTest client = new HttpTest("http://www.baidu.com");
      client.run();
     }
     public HttpTest(String urlString) {
      this.urlString = urlString;
     }
     public void run() throws Exception {
      //生成一个URL对象 
      URL url = new URL(urlString);
      //打开URL 
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      //得到输入流,即获得了网页的内容 
      BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection
        .getInputStream()));
      String line;
      // 读取输入流的数据,并显示
      while ((line = reader.readLine()) != null){
       System.out.println(line);
      }
     }
    }
  • 相关阅读:
    实习日记7.28
    实习日记7.27
    实习总结(第三周)
    实习日记7.26
    实习日记7.25
    实习总结(第二周)
    实习总结(第一周)
    实习日记7.22
    实习日记7.21
    5月4下
  • 原文地址:https://www.cnblogs.com/hellowzd/p/4985923.html
Copyright © 2011-2022 走看看