我很菜,曾为这个天气代码搞了好几天,不过现在我终于可以实现了
网站首页上每天都能够有最新的天气情况,而不用自己手动搜集,确实方便了很多,下面我就将我的经验介绍一下
关于天气服务,我是用的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;
}
}
其实只要自己认真分析分析,这个天气预报的抓取一点都不难~~~~