簡體   English   中英

如何從Python中的WeatherBug XML文件獲取價值?

[英]How to obtain value from WeatherBug XML File in 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

因此,相關數據如下:

<aws:temp units="&deg;F">30.3</aws:temp>

<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'}

實際上,當我進行喂食時,沒有提及溫度。 有人可以指出我正確的方向嗎? 我不熟悉python或feedparser,因此我對為什么在d.feed中未提及實際溫度數據(30.3度)感到困惑。

任何建議,不勝感激!

編輯:BeautifulSoup似乎是要走的路 我在搜索時從未遇到過。 再次感謝!

我的問題是: 這是解決此問題的首選方法嗎? 我要做的就是使用RRDTool繪制天氣數據圖表。 我使用WeatherBug的溫度,因為那是離我家最近的溫度。 這是首選的方式嗎? 目前,我有一個效率很低的bash腳本,它下載了weatherbug網頁,抓緊右行,並剪切數據。 暫時無法正常運行,因為有時數據超過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"
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似乎不能很好地處理解析,但是可以正常工作:

import feedparser
import string

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

print str(d.feed['aws_weather']).translate(None, string.ascii_letters)

我知道了:

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

現在我的腳本是這樣的:

import urllib
import sys
arg = sys.argv
from BeautifulSoup import BeautifulSoup as Soup
url='http://a6787859817.api.wxbug.net/getLiveWeatherRSS.aspx?ACode=A6787859817&zipCode=18657&OutputType=1'
handler=urllib.urlopen(url).read()
soup=Soup(handler)
temp=soup.find('aws:temp')
if "temp" in arg:
    print temp.text
d1=soup.find('aws:month')['number']
d2=soup.find('aws:day')['number']
d3=soup.find('aws:year')['number']
d4=soup.find('aws:hour')['number']
d5=soup.find('aws:minute')['number']
d6=soup.find('aws:am-pm')['abbrv']
mdy=d1+"/"+d2+"/"+d3
if "date" in arg:
    print mdy
hm=d4+":"+d5+" "+d6
if "hour" in arg:
    print hm

對於Conky來說確實非常好,很快我將嘗試RRDTOOL

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM