代碼:<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>
效果:
說(shuō)明:直接調(diào)用(原碼引用:http://www.tianqi123.com/)
名稱:根據(jù)IP地址顯示當(dāng)前城市的天氣預(yù)報(bào)代碼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>
效果:
說(shuō)明:直接調(diào)用(原碼引用:http://www.tianqi123.com/)
名稱:根據(jù)IP地址顯示當(dāng)前城市的天氣預(yù)報(bào)代碼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>
效果:
說(shuō)明:直接調(diào)用(原碼引用:http://www.tianqi123.com/)
天氣代碼高級(jí)使用方法: 如果您會(huì)HTML語(yǔ)言,您還可以在iframe代碼中的url后面加參數(shù),如: chengshi_321.html?c0=F9DC68&c1=white&c2=FEFCE0&t1=red&bg=white&w=178&h=250&text=no 其中 c0 表示 表格第一行背景顏色,c1,c2表示表格其他行間隔的背景顏色,t1表示 標(biāo)題顏色,bg 表示頁(yè)面北京顏色,w表示表格寬度 h 表示表格高度 當(dāng) text=yes 時(shí),將會(huì)出現(xiàn)滾動(dòng)的天氣文字,建議您自己調(diào)試看看,如: chengshi_321.html??c0=red&c1=FF9900&bg=F4FFF4&w=178&h=20&text=yes 注意:顏色請(qǐng)不要加 # 符號(hào),如 #FF9900 請(qǐng)寫成 FF9900 |
------------------------------------------------------------------------------------------------------------------
以下代碼(原碼引用:好多網(wǎng)站上都有)
名稱:265天氣根據(jù)IP自動(dòng)獲得當(dāng)?shù)氐奶鞖馇闆r
代碼:<iframe src="http://weather.265.com/weather.htm" width="168" height="54" frameborder="no" border="0" marginwidth="0" marginheight="0" scrolling="no"></iframe>
效果:
說(shuō)明:直接調(diào)用
-----------------------------------------------------------------------------------
名稱:QQ天氣預(yù)報(bào)代碼(一)
代碼:<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>
效果:
說(shuō)明:這種適合于在網(wǎng)頁(yè)的邊欄插入。但一個(gè)缺點(diǎn)是,上面的4個(gè)城市是既定的,無(wú)法改成別的。插 入時(shí),選好網(wǎng)頁(yè)上的位置,直接將左欄的源代碼全部拷進(jìn)去就行了
-------------------------------------------------------------------------------------------
名稱:QQ天氣預(yù)報(bào)代碼(二)
代碼:<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>
效果:
說(shuō)明:這種也適合于在網(wǎng)頁(yè)的邊欄插入。上面的城市可以自定,比如廈門可改成別的。定制的方法是修改我代碼中標(biāo)紅的數(shù)字,從1開(kāi)始代表“香港”開(kāi)始,每個(gè)數(shù)字都代表一個(gè)城市,廈門是287,具體要哪個(gè)城市自己找一下罷。
----------------------------------------------------------------------------------------------------------
名稱:新浪天氣預(yù)報(bào)代碼
代碼:<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>
效果:
說(shuō)明:這種適合于在網(wǎng)頁(yè)的頭欄插入。上面的城市可以自定,比如 石家莊可改成別的。
---------------------------------------------------------------------------------------------------------
名稱:QQ天氣預(yù)報(bào)代碼(四)
代碼:<iframe width=160 height=230 frameborder=0 scrolling=NO src=http://appnews.qq.com/cgi-bin/news_qq_search?city=南昌></iframe>
效果:
說(shuō)明:直接調(diào)用
------------------------------------------------------------------------------------------------------
自己從網(wǎng)頁(yè)中偷取天氣數(shù)據(jù)
主要用到Pattern類和Matcher類在java.util.regex.包下面。
首先在網(wǎng)頁(yè)上取得所有數(shù)據(jù)
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();
}
}
然后分析數(shù)據(jù)
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() {
//開(kāi)始分析返回的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;
}
}
// 獲取風(fēng)力
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;
}
}