<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

    解析網址提取天氣信息

    以前寫過的一個從網上提取天氣信息的類,參照了公司老前輩們的代碼,可能不太規范,但基本實現,主要就是對頁面源碼的解析和有用信息的截取,取出來得都是有規律的字符串信息,可根據需要存進數據庫,進行應用。代碼如下:
      
      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 閱讀(1569) 評論(1)  編輯  收藏

    評論

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

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


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


    網站導航:
     

    導航

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

    統計

    常用鏈接

    留言簿(2)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产尤物在线视精品在亚洲| 亚洲中文字幕无码久久2020| 一个人免费观看视频在线中文| 热99re久久精品精品免费| 亚洲av成人一区二区三区| 色窝窝免费一区二区三区| 色噜噜亚洲男人的天堂| 免费无遮挡无码视频网站| 亚洲AV无码专区在线厂| 亚洲成a人一区二区三区| 人碰人碰人成人免费视频| 国产亚洲一区二区三区在线观看| 嫩草在线视频www免费看| 亚洲欧洲视频在线观看| 成人爽A毛片免费看| 亚洲日本VA中文字幕久久道具| 国产大片线上免费看| 成人无码视频97免费| 亚洲视频一区在线| 在线免费观看视频你懂的| 黄色免费在线观看网址| 日韩va亚洲va欧洲va国产| 中文字幕乱码免费视频| 猫咪免费人成网站在线观看入口| 中文字幕无码精品亚洲资源网| 日韩精品无码专区免费播放| 激情五月亚洲色图| 亚洲区小说区图片区| 蜜臀AV免费一区二区三区| 最新亚洲人成无码网www电影| 国产AⅤ无码专区亚洲AV| 4399影视免费观看高清直播| 色综合久久精品亚洲国产| 亚洲综合日韩久久成人AV| 1区2区3区产品乱码免费| 国产av无码专区亚洲av毛片搜 | 日本不卡在线观看免费v| 久久久免费观成人影院| 亚洲一区二区三区国产精品无码 | 日韩吃奶摸下AA片免费观看| 羞羞视频在线观看免费|