随笔-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 阅读(602) 评论(0)  编辑  收藏 所属分类: java基础

只有注册用户登录后才能发表评论。


网站导航: