利用xml,解析yahoo天氣程式碼~~~
阿新 • • 發佈:2019-01-26
我很菜,曾為這個天氣程式碼搞了好幾天,不過現在我終於可以實現了
網站首頁上每天都能夠有最新的天氣情況,而不用自己手動蒐集,確實方便了很多,下面我就將我的經驗介紹一下
關於天氣服務,我是用的yahoo提供的天氣服務,網上搜索的時候,據說weather.com也提供這個服務,不過需要註冊,我去看看了,甚至連註冊的地方都沒找到(漢自己的e文阿),就懶得用他們家的了
yahoo的天氣服務地址是
http://xml.weather.yahoo.com/
在yahoo的技術支援裡對於天氣的程式碼有詳細的解釋
http://developer.yahoo.com/weather/
簡單說來,我們可以傳送請求到yahoo,它就會返回一個xml檔案,請求的地址如下:
http://xml.weather.yahoo.com/forecastrss?p=CHXX0008&u=c
我們得到的xml檔案格式如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Beijing, CH</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://xml.weather.yahoo.com/forecast/CHXX0008_c.html</link>
<description>Yahoo! Weather for Beijing, CH</description>
<language>en-us</language>
<lastBuildDate>Tue, 26 Dec 2006 12:00 pm CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Beijing" region="" country="CH" />
<yweather:units temperature="C" distance="km" pressure="mb" speed="kph" />
<yweather:wind chill="-7" direction="300" speed="32" />
<yweather:atmosphere humidity="27" visibility="999" pressure="0" rising="2" />
<yweather:astronomy sunrise="7:35 am" sunset="4:56 pm" />
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com/</link>
<url>http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
</image>
<item>
<title>Conditions for Beijing, CH at 12:00 pm CST</title>
<geo:lat>39.93</geo:lat>
<geo:long>116.28</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://xml.weather.yahoo.com/forecast/CHXX0008_c.html</link>
<pubDate>Tue, 26 Dec 2006 12:00 pm CST</pubDate>
<yweather:condition text="Fair" code="34" temp="0" date="Tue, 26 Dec 2006 12:00 pm CST" />
<description><![CDATA[
<img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/34.gif" /><br />
<b>Current Conditions:</b><br />
Fair, 0 C<BR /><BR />
<b>Forecast:</b><BR />
Tue - Sunny. High: 1 Low: -8<br />
Wed - Sunny. High: 1 Low: -9<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://xml.weather.yahoo.com/forecast/CHXX0008_c.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]></description>
<yweather:forecast day="Tue" date="26 Dec 2006" low="-8" high="1" text="Sunny" code="32" />
<yweather:forecast day="Wed" date="27 Dec 2006" low="-9" high="1" text="Sunny" code="32" />
<guid isPermaLink="false">CHXX0008_2006_12_26_12_0_CST</guid>
</item>
</channel>
</rss>
<!-- p6.weather.scd.yahoo.com uncompressed Tue Dec 26 21:04:50 PST 2006 -->
看到了吧,裡面有我們需要的資料,這時我們只要通過xml操作讀取到裡面的資料就可以了。
xml的操作我所知道有dom,sax api和jdom三種方式,隨便哪一種你都可以,我用了sax這種方式
Weather weather=new Weather();
Vector vector=new Vector();
YahooHandler yh=new YahooHandler();
URL url=new URL("http://localhost:8080/daemon/weather.xml");
InputStream input=url.openStream();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
SAXParser parser = factory.newSAXParser();
parser.parse(input, yh);
vector=yh.getVector();
weather=(Weather)vector.elementAt(0);
在這裡我有一個weather.class封裝了一些weather的屬性,一個yahoohandler用來解析資料
主要就是這麼一些語句:
public void startElement(String name,AttributeList attributes) throws SAXException{
String temp_date;
if(name.equalsIgnoreCase("item")){
weather=new Weather();
}else if(name.equalsIgnoreCase("yweather:condition")){
temp_date=attributes.getValue("date");
System.out.println("pubDate is :"+temp_date);
//weather.setPubDate();
}else if(name.equalsIgnoreCase("description")){
//System.out.println("When ther is description the temp_date is :"+temp_date);
}else if(name.equalsIgnoreCase("yweather:forecast")){
temp_date=attributes.getValue("date");
String day=attributes.getValue("day");
int low=Integer.parseInt(attributes.getValue("low"));
int high=Integer.parseInt(attributes.getValue("high"));
String text=attributes.getValue("text");
//將字串日期轉換為日期格式
if(temp_sign==0){
weather.setTodayDay(day);
weather.setTodayLow(low);
weather.setTodayHigh(high);
weather.setTodayText(text);
//System.out.println("Date is :"+temp_date+" day is :"+day+" low :"+low+" high :"+high+" text:"+text);
temp_sign=1;
}else if(temp_sign==1){
weather.setTomDay(day);
weather.setTomLow(low);
weather.setTomHigh(high);
weather.setTomText(text);
//System.out.println("Date is :"+temp_date+" day is :"+day+" low :"+low+" high :"+high+" text:"+text);
temp_sign=0;
}
}
其實只要自己認真分析分析,這個天氣預報的抓取一點都不難~~~~
網站首頁上每天都能夠有最新的天氣情況,而不用自己手動蒐集,確實方便了很多,下面我就將我的經驗介紹一下
關於天氣服務,我是用的yahoo提供的天氣服務,網上搜索的時候,據說weather.com也提供這個服務,不過需要註冊,我去看看了,甚至連註冊的地方都沒找到(漢自己的e文阿),就懶得用他們家的了
yahoo的天氣服務地址是
http://xml.weather.yahoo.com/
在yahoo的技術支援裡對於天氣的程式碼有詳細的解釋
http://developer.yahoo.com/weather/
簡單說來,我們可以傳送請求到yahoo,它就會返回一個xml檔案,請求的地址如下:
http://xml.weather.yahoo.com/forecastrss?p=CHXX0008&u=c
我們得到的xml檔案格式如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<rss version="2.0" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">
<channel>
<title>Yahoo! Weather - Beijing, CH</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://xml.weather.yahoo.com/forecast/CHXX0008_c.html</link>
<description>Yahoo! Weather for Beijing, CH</description>
<language>en-us</language>
<lastBuildDate>Tue, 26 Dec 2006 12:00 pm CST</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Beijing" region="" country="CH" />
<yweather:units temperature="C" distance="km" pressure="mb" speed="kph" />
<yweather:wind chill="-7" direction="300" speed="32" />
<yweather:atmosphere humidity="27" visibility="999" pressure="0" rising="2" />
<yweather:astronomy sunrise="7:35 am" sunset="4:56 pm" />
<image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com/</link>
<url>http://us.i1.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif</url>
</image>
<item>
<title>Conditions for Beijing, CH at 12:00 pm CST</title>
<geo:lat>39.93</geo:lat>
<geo:long>116.28</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://xml.weather.yahoo.com/forecast/CHXX0008_c.html</link>
<pubDate>Tue, 26 Dec 2006 12:00 pm CST</pubDate>
<yweather:condition text="Fair" code="34" temp="0" date="Tue, 26 Dec 2006 12:00 pm CST" />
<description><![CDATA[
<img src="http://us.i1.yimg.com/us.yimg.com/i/us/we/52/34.gif" /><br />
<b>Current Conditions:</b><br />
Fair, 0 C<BR /><BR />
<b>Forecast:</b><BR />
Tue - Sunny. High: 1 Low: -8<br />
Wed - Sunny. High: 1 Low: -9<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Beijing__CH/*http://xml.weather.yahoo.com/forecast/CHXX0008_c.html">Full Forecast at Yahoo! Weather</a><BR/>
(provided by The Weather Channel)<br/>
]]></description>
<yweather:forecast day="Tue" date="26 Dec 2006" low="-8" high="1" text="Sunny" code="32" />
<yweather:forecast day="Wed" date="27 Dec 2006" low="-9" high="1" text="Sunny" code="32" />
<guid isPermaLink="false">CHXX0008_2006_12_26_12_0_CST</guid>
</item>
</channel>
</rss>
<!-- p6.weather.scd.yahoo.com uncompressed Tue Dec 26 21:04:50 PST 2006 -->
看到了吧,裡面有我們需要的資料,這時我們只要通過xml操作讀取到裡面的資料就可以了。
xml的操作我所知道有dom,sax api和jdom三種方式,隨便哪一種你都可以,我用了sax這種方式
Weather weather=new Weather();
Vector vector=new Vector();
YahooHandler yh=new YahooHandler();
URL url=new URL("http://localhost:8080/daemon/weather.xml");
InputStream input=url.openStream();
SAXParserFactory factory = SAXParserFactory.newInstance();
factory.setNamespaceAware(false);
SAXParser parser = factory.newSAXParser();
parser.parse(input, yh);
vector=yh.getVector();
weather=(Weather)vector.elementAt(0);
在這裡我有一個weather.class封裝了一些weather的屬性,一個yahoohandler用來解析資料
主要就是這麼一些語句:
public void startElement(String name,AttributeList attributes) throws SAXException{
String temp_date;
if(name.equalsIgnoreCase("item")){
weather=new Weather();
}else if(name.equalsIgnoreCase("yweather:condition")){
temp_date=attributes.getValue("date");
System.out.println("pubDate is :"+temp_date);
//weather.setPubDate();
}else if(name.equalsIgnoreCase("description")){
//System.out.println("When ther is description the temp_date is :"+temp_date);
}else if(name.equalsIgnoreCase("yweather:forecast")){
temp_date=attributes.getValue("date");
String day=attributes.getValue("day");
int low=Integer.parseInt(attributes.getValue("low"));
int high=Integer.parseInt(attributes.getValue("high"));
String text=attributes.getValue("text");
//將字串日期轉換為日期格式
if(temp_sign==0){
weather.setTodayDay(day);
weather.setTodayLow(low);
weather.setTodayHigh(high);
weather.setTodayText(text);
//System.out.println("Date is :"+temp_date+" day is :"+day+" low :"+low+" high :"+high+" text:"+text);
temp_sign=1;
}else if(temp_sign==1){
weather.setTomDay(day);
weather.setTomLow(low);
weather.setTomHigh(high);
weather.setTomText(text);
//System.out.println("Date is :"+temp_date+" day is :"+day+" low :"+low+" high :"+high+" text:"+text);
temp_sign=0;
}
}
其實只要自己認真分析分析,這個天氣預報的抓取一點都不難~~~~