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个数
    
 }
 

}

 

posted @ 2015-11-24 15:39  fleam  阅读(2062)  评论(0编辑  收藏  举报