解析網址提取天氣信息

          以前寫過的一個從網上提取天氣信息的類,參照了公司老前輩們的代碼,可能不太規范,但基本實現,主要就是對頁面源碼的解析和有用信息的截取,取出來得都是有規律的字符串信息,可根據需要存進數據庫,進行應用。代碼如下:
            
            1 package parsehtml;
            2 
            3 import java.io.BufferedReader;
            4 import java.io.InputStreamReader;
            5 import java.net.HttpURLConnection;
            6 import java.net.URL;
            7 import java.util.ArrayList;
            8 import java.util.Iterator;
            9 import java.util.List;
           10 
           11 public class ParseHtml extends Thread {
           12     /*
           13      * 解析網址運行
           14      * 
           15      */public void run() {
           16         try {
           17             // 河北天氣
           18             String urlAddress = "http://www.weathercn.com/forecast/province.jsp?province=hebei";
           19             startParse(urlAddress);
           20         } catch (Exception e) {
           21             e.printStackTrace();
           22             System.out.println("網絡錯誤,提取天氣數據出錯!");
           23         }
           24     }
           25 
           26     /*
           27      * 
           28      * 開始解析網址
           29      * 
           30      * 
           31      */public void startParse(String urlAddress) throws Exception {
           32         System.out.println("開始提取網址:" + urlAddress);
           33         URL url = new URL(urlAddress);
           34         HttpURLConnection httpConnection = (HttpURLConnection) url
           35                 .openConnection();
           36         httpConnection.setRequestProperty("User-Agent""Mozilla");
           37         httpConnection.setRequestProperty("Connection""Keep-Alive");
           38 
           39         int responseCode = 0;
           40         try {
           41             responseCode = httpConnection.getResponseCode();
           42         } catch (Exception ex) {
           43             System.out.println("讀取網頁失敗,返回代碼:" + responseCode);
           44         }
           45         System.out.println("讀取網頁反回代碼:" + responseCode);
           46 
           47         // 獲得輸入流
           48         InputStreamReader ir = new InputStreamReader(httpConnection
           49                 .getInputStream());
           50         if (ir != null) {
           51             BufferedReader reader = new BufferedReader(ir);
           52             System.out.println(reader);
           53             if (reader != null)
           54                 // 調用從何處取數據
           55                 isStartPoint(reader, "99"1);
           56             reader.close();
           57             ir.close();
           58         }
           59 
           60     }
           61 
           62     private void isStartPoint(BufferedReader reader, String tag, int number)
           63             throws Exception {
           64         String CurrentLine = "";
           65 
           66         // 從流中讀取一行字符串(html源文件)
           67         while ((CurrentLine = reader.readLine()) != null) {
           68 
           69             // 循環查詢整個 CurrentLine 中的 tag,查到一個就將計數據器 number 減 1
           70             int fromIndex = 0;
           71             while ((number != 0)
           72                     && (CurrentLine.toUpperCase().indexOf(tag.toUpperCase(),
           73                             fromIndex) != -1)) {
           74                 fromIndex = CurrentLine.toUpperCase().indexOf(
           75                         tag.toUpperCase(), fromIndex) + 1;
           76                 if (fromIndex > 0)
           77                     number--;
           78             }
           79 
           80             // 如果到了起始點即 number == 0 時,開始執行取數據操作
           81             List sb = new ArrayList();
           82             if ((CurrentLine.indexOf("citydetail"> 0)
           83                     && (CurrentLine.indexOf("99"> 0)) {
           84                 sb.add(this.processBuffer(CurrentLine));
           85                 // 截取天氣信
           86                 CurrentLine = reader.readLine();
           87                 CurrentLine = reader.readLine();
           88                 if (CurrentLine != null) {
           89                     sb.add(this.processBuffer(CurrentLine));
           90                 }
           91                 // 截取最低氣溫
           92                 CurrentLine = reader.readLine();
           93                 if (CurrentLine != null) {
           94                     sb.add(this.processBuffer(CurrentLine));
           95                 }
           96             }
           97             StringBuffer s = new StringBuffer();
           98             // 將所有的截取信息匯總進行處理,用‘,’間隔便于以后截取相應信息
           99             for (Iterator it = sb.iterator(); it.hasNext();) {
          100                 String i = it.next().toString();
          101                 s.append(i);
          102             }
          103             String Tq = s.toString();
          104             String[] Tqxx = Tq.split(",");
          105             if (Tqxx.length >= 3) {
          106                 System.out.println(Tq);
          107             }
          108         }
          109     }
          110 
          111     /*
          112      * 判斷并從網頁上截取
          113      * 
          114      * @param old
          115      */
          116     private String processBuffer(String strLine) throws Exception {
          117         // 保存當前取得的 城市
          118         StringBuffer sb = new StringBuffer();
          119         String Tqxx;
          120         // 當當前行含有“城市”時,截取相應的城市名稱
          121         if (strLine.indexOf("citydetail"> 0) {
          122             Tqxx = subString(strLine, "sta_id""<");
          123             Tqxx = Tqxx.substring(24);
          124             sb = sb.append(Tqxx + ",");
          125         }
          126         if (strLine.indexOf("alt"> 0) {
          127             Tqxx = subString(strLine, "alt"">");
          128             Tqxx = Tqxx.substring(1);
          129             sb = sb.append(Tqxx + ",");
          130         }
          131         if (strLine.indexOf("strong"> 0) {
          132             strLine = strLine.replaceAll(" """);
          133             Tqxx = subString(strLine, "strong>""<");
          134             String Tqxx1 = subString(strLine, "-""</");
          135             Tqxx1 = Tqxx1.substring(8);
          136             Tqxx = Tqxx + "~" + Tqxx1;
          137             sb = sb.append(Tqxx + ",");
          138         }
          139         return sb.toString();
          140 
          141     }
          142 
          143     /*
          144      * 返回在 strSourc 的 strStart ,strEnd 之間的字符串
          145      * 
          146      */
          147     private String subString(String strSource, String strStart, String strEnd) {
          148         strSource = strSource.toUpperCase();
          149         strStart = strStart.toUpperCase();
          150         strEnd = strEnd.toUpperCase();
          151         int intStart = strSource.indexOf(strStart);
          152         int intEnd = strSource.indexOf(strEnd, intStart);
          153         String strRetu = " ";
          154         if (intStart == -1)
          155             return strRetu;
          156         if ((intEnd != -1&& (intEnd > intStart)) {
          157             strRetu = strSource.substring(intStart + strStart.length(), intEnd);
          158         } else {
          159             strRetu = strSource.substring(intStart + strStart.length());
          160         }
          161         return strRetu.trim();
          162     }
          163 
          164     public ParseHtml() {
          165 
          166     }
          167 
          168     public static void main(String args[]) {
          169         ParseHtml p = new ParseHtml();
          170         p.run();
          171     }
          172 }
          173 

          posted on 2007-04-30 09:47 reeve 閱讀(1575) 評論(1)  編輯  收藏

          評論

          # re: 解析網址提取天氣信息[未登錄] 2007-04-30 09:59 samuel

          呵呵!早就在3年前玩過了。。。你可以查看httpclient開源項目。。  回復  更多評論   


          只有注冊用戶登錄后才能發表評論。


          網站導航:
           

          導航

          <2007年4月>
          25262728293031
          1234567
          891011121314
          15161718192021
          22232425262728
          293012345

          統計

          常用鏈接

          留言簿(2)

          隨筆檔案

          搜索

          最新評論

          閱讀排行榜

          評論排行榜

          主站蜘蛛池模板: 黄浦区| 元江| 宁强县| 桦川县| 彩票| 铜梁县| 阿鲁科尔沁旗| 天气| 泰宁县| 陆河县| 广河县| 观塘区| 抚宁县| 晋中市| 辽源市| 朔州市| 和田市| 宁南县| 渝中区| 罗定市| 鄱阳县| 徐州市| 孝感市| 五原县| 五台县| 甘洛县| 龙川县| 吉安市| 泸水县| 巴彦淖尔市| 蛟河市| 修文县| 嵊州市| 河北区| 诸城市| 凤凰县| 富平县| 舒兰市| 仙游县| 永德县| 库尔勒市|