如何从Python中的WeatherBug XML文件中获取值?

2024-04-30 03:39:36 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从以下文件中获取温度和时间:

<rss xmlns:georss="http://www.georss.org/georss" version="2.0">
<channel>
<title>Observations from Tunkhannock, PA - USA</title>
<link>
http://weather.weatherbug.com/PA/Tunkhannock-weather.html?ZCode=Z5546&Units=0&stat=TNKCN
</link>
<description>
Weatherbug, the owner of the world's largest weather network is now providing an API to it's weather data in the form of RSS. This will enable it's enthusiastic users to build their own applications.
</description>
<language>en-us</language>
<lastBuildDate>Sat, 05 Jan 2013 01:00:00 GMT</lastBuildDate>
<ttl>60</ttl>
<aws:weather xmlns:aws="http://www.aws.com/aws">
<aws:api version="2.0"/>
<aws:WebURL>
http://weather.weatherbug.com/PA/Tunkhannock-weather.html?ZCode=Z5546&Units=0&stat=TNKCN
</aws:WebURL>
<aws:InputLocationURL>
http://weather.weatherbug.com/PA/Tunkhannock-weather.html?ZCode=Z5546&Units=0
</aws:InputLocationURL>
<aws:station requestedID="" id="TNKCN" name="Tunkhannock HS" city="Tunkhannock" state=" PA" zipcode="18657" country="USA" latitude="41.5663871765137" longitude="-75.9794464111328"/>
<aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons/cond002.gif">Partly Cloudy</aws:current-condition>
<aws:temp units="&deg;F">30.3</aws:temp>
<aws:rain-today units=""">0</aws:rain-today>
<aws:wind-speed units="mph">1</aws:wind-speed>
<aws:wind-direction>WNW</aws:wind-direction>
<aws:gust-speed units="mph">20</aws:gust-speed>
<aws:gust-direction>WNW</aws:gust-direction>
</aws:weather>
<image>
<title>Local Weather from WeatherBug</title>
<width>142</width>
<height>18</height>
<link>
http://weather.weatherbug.com/PA/Tunkhannock-weather.html?ZCode=Z5546&Units=0&stat=TNKCN
</link>
<url>
http://www.weatherbug.com/aws/imagesHmPg0604/img_wxbug_logo_whiteBG.gif
</url>
</image>
<item>
<title>Live Conditions from Tunkhannock, PA - USA</title>
<link>
http://weather.weatherbug.com/PA/Tunkhannock-weather.html?ZCode=Z5546&Units=0&stat=TNKCN
</link>
<pubDate>Sat, 05 Jan 2013 01:54:00 GMT</pubDate>
<description>
<![CDATA[
<img src="http://deskwx.weatherbug.com/images/Forecast/icons/cond002.gif" border="0" alt="Current Conditions"/>&nbsp;&nbsp;&nbsp;
 <b>Partly Cloudy</b> <br />

 <b>Temperature:</b> 30.3 &deg;F&nbsp;&nbsp;    
 <br />
 <b>Wind Speed:</b> 1 mph WNW&nbsp;&nbsp;
 <br /> 
 <b>Gusts:</b> 20 mph WNW &nbsp;&nbsp;
 <b>Rain Today:</b> 0 &quot; &nbsp;&nbsp;
 <br />
]]>
</description>
<georss:point>41.5663871765137 -75.9794464111328</georss:point>
<guid isPermaLink="false">Sat, 05 Jan 2013 01:54:21 GMT-Station1</guid>
</item>
</channel>
</rss

所以相关数据应该是:

^{pr2}$

以及

<pubDate>Sat, 05 Jan 2013 01:54:00 GMT</pubDate>

我已经在谷歌上搜了好几个小时了。我尝试过feedparser:

import feedparser
d = feedparser.parse('http://api.wxbug.net/getLiveCompactWeatherRSS.aspx?ACode=A6787859817&zipcode=18657&unittype=0')

但是做

d.feed.temperature 

不管用(当然)。以及

d.feed.aws_temp

简单地返回

{'units': u'&deg;F'}

事实上,当我做d.feed的时候,里面没有提到温度。有人能给我指出正确的方向吗?我不熟悉python或feedparser,所以我很困惑为什么d.feed中没有提到实际温度数据(30.3度)。在

如有任何建议,我们将不胜感激!!在

编辑:beauthoulsoup似乎是一种可行的方法;我在搜索时从未遇到过。再次感谢!在

我的问题是:这是进行这项工作的首选方式吗?我只想用RRDTool绘制天气数据。我用WeatherBug的温度,因为那离我家最近。这是首选的方法吗?目前我有一个非常低效的bash脚本,下载weatherbug网页,greps为正确的行,并削减数据。因为有时数据超过4个字符,所以暂时不太好用!在

#/bin/bash
wget -q -O /home/colby/Scripts/conkyscripts/weather/fullPage "http://weather.weatherbug.com/PA/Tunkhannock-weather.html?zcode=z6286"

Time=`grep 'divObsTime' /home/colby/Scripts/conkyscripts/weather/fullPage | cut -c 41-49`

Temp=`grep -i 'divtemp' /home/colby/Scripts/conkyscripts/weather/fullPage | cut -c 59-62`

echo "As of $Time, it is $Temp"

Tags: comawshttptitlehtmllinkgeorssunits
2条回答

我明白了:

import urllib
from BeautifulSoup import BeautifulSoup as Soup
>>> url='http://api.wxbug.net/getLiveCompactWeatherRSS.aspx?ACode=A6787859817&zipcode=18657&unittype=0'
>>> handler=urllib.urlopen(url).read()
>>> soup=Soup(handler)
>>> data=soup.find('aws:temp')
>>> print data
<aws:temp units="&deg;F">35.8</aws:temp>
>>> print data.text
35.8

现在我的剧本是这样写的:

^{pr2}$

工作真的,真的很好的康基,很快我将尝试使用RRDTOOL

from BeautifulSoup import BeautifulSoup as Soup

file = 'data.xml'
handler = open(file).read()

soup = Soup(handler)

data = soup.find('aws:temp')
print data.text

FeedParser似乎不能很好地处理解析,但这是可行的:

^{pr2}$

相关问题 更多 >