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

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

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

    解析網(wǎng)址提取天氣信息

    以前寫過的一個從網(wǎng)上提取天氣信息的類,參照了公司老前輩們的代碼,可能不太規(guī)范,但基本實現(xiàn),主要就是對頁面源碼的解析和有用信息的截取,取出來得都是有規(guī)律的字符串信息,可根據(jù)需要存進(jìn)數(shù)據(jù)庫,進(jìn)行應(yīng)用。代碼如下:
      
      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      * 解析網(wǎng)址運(yùn)行
     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("網(wǎng)絡(luò)錯誤,提取天氣數(shù)據(jù)出錯!");
     23         }
     24     }
     25 
     26     /*
     27      * 
     28      * 開始解析網(wǎng)址
     29      * 
     30      * 
     31      */public void startParse(String urlAddress) throws Exception {
     32         System.out.println("開始提取網(wǎng)址:" + 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("讀取網(wǎng)頁失敗,返回代碼:" + responseCode);
     44         }
     45         System.out.println("讀取網(wǎng)頁反回代碼:" + 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                 // 調(diào)用從何處取數(shù)據(jù)
     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             // 循環(huán)查詢整個 CurrentLine 中的 tag,查到一個就將計數(shù)據(jù)器 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 時,開始執(zhí)行取數(shù)據(jù)操作
     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             // 將所有的截取信息匯總進(jìn)行處理,用‘,’間隔便于以后截取相應(yīng)信息
     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      * 判斷并從網(wǎng)頁上截取
    113      * 
    114      * @param old
    115      */
    116     private String processBuffer(String strLine) throws Exception {
    117         // 保存當(dāng)前取得的 城市
    118         StringBuffer sb = new StringBuffer();
    119         String Tqxx;
    120         // 當(dāng)當(dāng)前行含有“城市”時,截取相應(yīng)的城市名稱
    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 閱讀(1570) 評論(1)  編輯  收藏

    評論

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

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


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


    網(wǎng)站導(dǎo)航:
     

    導(dǎo)航

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

    統(tǒng)計

    常用鏈接

    留言簿(2)

    隨筆檔案

    搜索

    最新評論

    閱讀排行榜

    評論排行榜

    主站蜘蛛池模板: 国产aa免费视频| 亚洲AV日韩AV永久无码下载| 91av免费在线视频| 亚洲AV无码久久精品狠狠爱浪潮| 最新中文字幕免费视频| 精品熟女少妇aⅴ免费久久| 亚洲欧洲日产国产最新| 免费萌白酱国产一区二区| 久久久久久久99精品免费| 亚洲精品无播放器在线播放 | 亚洲国产精品va在线播放| 成人毛片免费观看视频| 两个人www免费高清视频| 在线精品亚洲一区二区| 久久精品国产亚洲综合色 | 亚洲AV永久无码精品| 成人午夜性A级毛片免费| 成人网站免费看黄A站视频| 亚洲欧美日韩一区二区三区| 久久亚洲精品成人| 免费在线观看的黄色网址| 男女超爽刺激视频免费播放| 久久性生大片免费观看性| 亚洲人成网站色7799| 亚洲色四在线视频观看| 亚洲爽爽一区二区三区| 香蕉高清免费永久在线视频 | 日本免费人成黄页网观看视频 | 国产精品亚洲成在人线| 国产国产人免费人成免费视频| 久久WWW免费人成一看片| 抽搐一进一出gif免费视频| 久久亚洲色WWW成人欧美| 亚洲人成在线播放| 亚洲av综合色区| 区三区激情福利综合中文字幕在线一区亚洲视频1 | 亚洲欧美日韩中文无线码 | 自拍偷自拍亚洲精品第1页 | 亚洲中文字幕无码爆乳| 亚洲网站免费观看| 久久香蕉国产线看观看亚洲片|