• java 获取网页指定内容-2(实践+修改)


    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.Arrays;
    
    
    public class Weather {
     String urlString;
     String array;
     StringBuffer sb=new StringBuffer("");
      
     public static void main(String[] args) throws Exception {
      Weather client = new Weather("http://www.weather.com.cn/weather/101181201.shtml");
      client.run();
     }
     public Weather(String urlString) {
      this.urlString = urlString;
     }
     public void run() throws Exception {
     
      URL url = new URL(urlString);
      
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      
      BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection
        .getInputStream(),"utf8"));
      String line;
    
      while ((line = reader.readLine()) != null){
      Pattern p = Pattern.compile("<p class="wea">(.+?)</p>");
        Matcher m = p.matcher(line);
        while(m.find()) { 
            array = m.group(1);
            sb.append(array+","); 
        }
      }
      
        String arr = sb.toString();
        String[] s = arr.split("\,");
        System.out.println(s[s.length - 7]);
        
     }
     
    
    }

    utf8编码格式

    import java.io.BufferedReader;
    import java.io.InputStreamReader;
    import java.net.HttpURLConnection;
    import java.net.URL;
    import java.util.regex.Matcher;
    import java.util.regex.Pattern;
    import java.util.Arrays;
    
    
    public class Weather {
     String urlString;
     String array;
     StringBuffer sb=new StringBuffer("");
      
     public static void main(String[] args) throws Exception {
      
      Weather client = new Weather("http://www.weather.com.cn/weather/101181201.shtml");
      client.run();
     }
     public Weather(String urlString) {
      this.urlString = urlString;
     }
     public void run() throws Exception {
     
      URL url = new URL(urlString);
      
      HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
      
      BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection
        .getInputStream(),"utf8"));
      String line;
    
      while ((line = reader.readLine()) != null){
      Pattern p = Pattern.compile("<p class="wea">(.+?)</p>");
        Matcher m = p.matcher(line);
        while(m.find()) { 
            array = m.group(1);
            sb.append(array+",");//符合正则的数据追加到sb,并以逗号分割 
        }
      }
      
        String arr = sb.toString();//sb转为字符串
        String[] s = arr.split("\,");//字符串转为数组,以逗号为标记 
        System.out.println(s[s.length - 7]);//取数组中倒数第7个数
        
     }
     
    
    }
  • 相关阅读:
    2019.9.6文艺理论笔记
    2019.9.5作业整理
    2019下高级英语笔记
    2019下实用口才课笔记
    【学英语~磨耳朵】2013年以来看过的所有美剧&电影&纪录片等等
    python温度转换代码
    python蟒蛇绘制的代码以及目前还不知道怎么用的RGB颜色对照表
    看TED演讲——Why you will fail to have a great career
    结构与算法(5)-----队列
    结构与算法(4)-----栈
  • 原文地址:https://www.cnblogs.com/hellowzd/p/4991796.html
Copyright © 2020-2023  润新知