隨筆-0  評論-3  文章-28  trackbacks-0
          名稱:根據IP地址顯示當前城市的天氣預報代碼1
          代碼:<iframe src="http://www.tianqi123.com/php/current_city.php" width=178 height=248 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no align=center id=url></iframe>
          效果
          說明:直接調用(原碼引用:http://www.tianqi123.com/)

          名稱
          根據IP地址顯示當前城市的天氣預報代碼2
          代碼:<iframe src="http://www.tianqi123.com/php/current_city.php?c0=F9DC68&c1=white&c2=FEFCE0&t1=red&bg=white&w=178&text=no" width=178 height=248 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no align=center id=url></iframe>
          效果
          說明:直接調用(原碼引用:http://www.tianqi123.com/)

          名稱:根據IP地址顯示當前城市的天氣預報代碼3
          代碼:<iframe src="http://www.tianqi123.com/php/current_city.php?c0=red&c1=D96C00&bg=F4FFF4&w=178&h=20&text=yes" width=178 height=21 marginwidth=0 marginheight=0 hspace=0 vspace=0 frameborder=0 scrolling=no align=center id=url></iframe>
          效果
          說明:直接調用(原碼引用:http://www.tianqi123.com/)

          天氣代碼高級使用方法:
           
          如果您會HTML語言,您還可以在iframe代碼中的url后面加參數,如:
          chengshi_321.html?c0=F9DC68&c1=white&c2=FEFCE0&t1=red&bg=white&w=178&h=250&text=no
           
          其中 c0 表示 表格第一行背景顏色,c1,c2表示表格其他行間隔的背景顏色,t1表示 標題顏色,bg 表示頁面北京顏色,w表示表格寬度 h 表示表格高度
           
          當 text=yes 時,將會出現滾動的天氣文字,建議您自己調試看看,如:
          chengshi_321.html??c0=red&c1=FF9900&bg=F4FFF4&w=178&h=20&text=yes
           
          注意:顏色請不要加 # 符號,如 #FF9900 請寫成 FF9900

          ------------------------------------------------------------------------------------------------------------------
          以下代碼(原碼引用:好多網站上都有)


          名稱:265天氣根據IP自動獲得當地的天氣情況
          代碼:<iframe src="http://weather.265.com/weather.htm" width="168" height="54" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
          效果

          說明:直接調用
          -----------------------------------------------------------------------------------

          名稱QQ天氣預報代碼(一)
          代碼:<iframe width="145" height="130" border="0" align="center" marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0" scrolling="no" src="http://minisite.qq.com/Weather/news_new.h tml" allowTransparency="true"></iframe>
          效果


          說明:這種適合于在網頁的邊欄插入。但一個缺點是,上面的4個城市是既定的,無法改成別的。插 入時,選好網頁上的位置,直接將左欄的源代碼全部拷進去就行了

          -------------------------------------------------------------------------------------------

          名稱QQ天氣預報代碼(二)
          代碼:<IFRAME ID='ifm2' WIDTH='189' HEIGHT='190' ALIGN='CENTER' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO' SRC='http://weather.qq.com/inc/ss258.htm'></IFRAME>
          效果


          說明:這種也適合于在網頁的邊欄插入。上面的城市可以自定,比如廈門可改成別的。定制的方法是修改我代碼中標紅的數字,從1開始代表“香港”開始,每個數字都代表一個城市,廈門是287,具體要哪個城市自己找一下罷。

          ----------------------------------------------------------------------------------------------------------

          名稱:新浪天氣預報代碼
          代碼:<IFRAME ID='ifm2' WIDTH='260' HEIGHT='70' ALIGN='CENTER' MARGINWIDTH='0' MARGINHEIGHT='0' HSPACE='0' VSPACE='0' FRAMEBORDER='0' SCROLLING='NO' src="http://news.sina.com.cn/iframe/weather/130101.html"></iframe>
          效果

          說明:這種適合于在網頁的頭欄插入。上面的城市可以自定,比如 石家莊可改成別的。

          ---------------------------------------------------------------------------------------------------------

          名稱:QQ天氣預報代碼(四)
          代碼:<iframe width=160 height=230 frameborder=0 scrolling=NO src=http://appnews.qq.com/cgi-bin/news_qq_search?city=南昌></iframe>
          效果


          說明:直接調用

          ------------------------------------------------------------------------------------------------------

          自己從網頁中偷取天氣數據
          主要用到Pattern類和Matcher類在java.util.regex.包下面。
          首先在網頁上取得所有數據
          HttpURLConnection conn = null;
                  String outstr = "";
           try {
                      String strUrl = "http://www.wopos.com/MainSet/WeatherSet.aspx?city="+address;
                      URL url = new URL(strUrl);
                      conn = (HttpURLConnection) url.openConnection();
                      conn.setRequestMethod("GET");
                      BufferedReader br = new BufferedReader(new InputStreamReader(conn.
                              getInputStream(), "GBK"));
                      String line = "";
                      while ((line = br.readLine()) != null) {
                          outstr = outstr + line;
                      }
                      br.close();
                  } catch (Exception ex) {
                      ex.printStackTrace(System.out);
                  } finally {
                      if (conn != null) {
                          conn.disconnect();
                      }
                  }
          然后分析數據
          try{
                      Pattern p = Pattern.compile("<span id=\"Label\\d\" style=\"display:inline-block;\"><font size=\"\\d\">[\u4e00-\u9fa5]*\\d*[℃]*[~]*[.]*[-]*\\d*[℃]*[-]*\\d*[.]*\\d*[:]*[\u4e00-\u9fa5]*</font></span>");
                      Matcher m = p.matcher(outstr);
                      while (m.find()) {
                          String s = m.group();
          //                System.out.println("s=" + s);
                          if(s!=null){
                              hm.put(s.substring(10, 16), s);
                          }
                      }
                  }catch(Exception ex){
                      ex.printStackTrace(System.out);
                  }
          完整的例子為

          public class WoWeatherInit {

           Weather weather = new Weather();
           SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd");
           String time=df.format(date);
           public void getWeatherInit() {
                  //開始分析返回的xml
                  HttpURLConnection conn = null;
                  String outstr = "";
                  String outstrtest = "";
                  ObjectInputStream input = null;
                  HashMap hm = new HashMap();
                  HashMap hm1 = new HashMap();
                 
                  try {
                      String strUrl = "http://www.wopos.com/MainSet/WeatherSet.aspx?city="+address;
                      URL url = new URL(strUrl);
                      conn = (HttpURLConnection) url.openConnection();
                      conn.setRequestMethod("GET");
                      BufferedReader br = new BufferedReader(new InputStreamReader(conn.
                              getInputStream(), "GBK"));
                      String line = "";
                      while ((line = br.readLine()) != null) {
                          outstr = outstr + line;
                      }
                      br.close();
                  } catch (Exception ex) {
                      ex.printStackTrace(System.out);
                  } finally {
                      if (conn != null) {
                          conn.disconnect();
                      }
                  }
                  if(outstr == null){
                      System.out.println("null");
                  }
                  try{
                      Pattern p = Pattern.compile("<span id=\"Label\\d\" style=\"display:inline-block;\"><font size=\"\\d\">[\u4e00-\u9fa5]*\\d*[℃]*[~]*[.]*[-]*\\d*[℃]*[-]*\\d*[.]*\\d*[:]*[\u4e00-\u9fa5]*</font></span>");
                      Matcher m = p.matcher(outstr);
                      while (m.find()) {
                          String s = m.group();
          //                System.out.println("s=" + s);
                          if(s!=null){
                              hm.put(s.substring(10, 16), s);
                          }
                      }
                  }catch(Exception ex){
                      ex.printStackTrace(System.out);
                  }

                  String sAddress = getAddress(hm);
                  String sDate = getDate(hm);
                  String sTianQi = getTianQi(hm);
                  String sWenDu = getWenDu(hm);
                  String sWind = getWind(hm);
              }

              //獲取地址
              public String getAddress(HashMap hm){
               String sValue = "";
                  try {
                      String sText = (String)hm.get("Label5");
                      String sFont = "<font size=\"2\">";
                      int iBegin = sText.indexOf(sFont);
                      int iEnd = sText.indexOf("</font>");
                      sValue = sText.substring(iBegin+sFont.length(), iEnd);
                  } catch (Exception ex) {
                      ex.printStackTrace(System.out);
                      sValue="";
                  } finally{
                   return sValue;
                  }
              }

              //獲取天氣
              public String getTianQi(HashMap hm){
               String sValue = "";
                  try {
                      String sText = (String)hm.get("Label1");
                      String sFont = "<font size=\"2\">";
                      int iBegin = sText.indexOf(sFont);
                      int iEnd = sText.indexOf("</font>");
                      sValue = sText.substring(iBegin+sFont.length(), iEnd);
                  } catch (Exception ex) {
                      ex.printStackTrace(System.out);
                      sValue="";
                  } finally{
                   return sValue;
                  }
             }

          //  獲取日期
              public String getDate(HashMap hm){
               String sValue = "";
                 try {
                     String sText = (String)hm.get("Label7");
                     String sFont = "<font size=\"2\">";
                     int iBegin = sText.indexOf(sFont);
                     int iEnd = sText.indexOf("</font>");
                     sValue = sText.substring(iBegin+sFont.length(), iEnd);
                 } catch (Exception ex) {
                     ex.printStackTrace(System.out);
                     sValue="";
                 } finally{
                  return sValue;
                 }
             }
             
             //獲取溫度
             public String getWenDu(HashMap hm){
              String sValue = "";
                 try {
                     String sText = (String)hm.get("Label2");
                     String sFont = "<font size=\"2\">";
                     int iBegin = sText.indexOf(sFont);
                     int iEnd = sText.indexOf("</font>");
                     sValue = sText.substring(iBegin+sFont.length(), iEnd);
                 } catch (Exception ex) {
                     ex.printStackTrace(System.out);
                     sValue="";
                 } finally{
                  return sValue;
                 }
            }

          // 獲取風力
             public static String getWind(HashMap hm){
              String sValue = "";
                 try {
                     String sText = (String)hm.get("Label3");
                     String sFont = "<font size=\"2\">";
                     int iBegin = sText.indexOf("<font size=\"2\">");
                     int iEnd = sText.indexOf("</font>");
                     sValue = sText.substring(iBegin+sFont.length(), iEnd);
                 } catch (Exception ex) {
                     ex.printStackTrace(System.out);
                 }
                 return sValue;
             }
          }


          posted on 2008-01-24 09:37 閱讀(627) 評論(0)  編輯  收藏 所屬分類: java基礎
          主站蜘蛛池模板: 濮阳市| 定边县| 大冶市| 共和县| 东港市| 搜索| 大埔县| 永宁县| 银川市| 浦县| 新乡市| 临沂市| 四会市| 建昌县| 南投市| 乌苏市| 富川| 桂东县| 安国市| 竹北市| 濮阳市| 禄丰县| 新昌县| 友谊县| 扬中市| 陇西县| 南陵县| 墨玉县| 福州市| 平谷区| 诸城市| 安塞县| 白城市| 武冈市| 天气| 南涧| 万山特区| 阿勒泰市| 汪清县| 隆林| 十堰市|