我正在使用谷歌网站检索天气信息,我想在XML标签之间找到值.下面的代码给我一个城市的天气状况,但我无法获得其他参数,如温度,如果可能的话,解释代码中隐含的分离函数的工作:
import urllib
def getWeather(city):
#create google weather api url
url = "http://www.google.com/ig/api?weather=" + urllib.quote(city)
try:
# open google weather api url
f = urllib.urlopen(url)
except:
# if there was an error opening the url, return
return "Error opening url"
# read contents to a string
s = f.read()
# extract weather condition data from xml string
weather = s.split("<current_conditions><condition data=\"")[-1].split("\"")[0]
# if there was an error getting the condition, the city is invalid
if weather == "<?xml version=":
return "Invalid city"
#return the weather condition
return weather
def main():
while True:
city = raw_input("Give me a city: ")
weather = getWeather(city)
print(weather)
if __name__ == "__main__":
main()
Run Code Online (Sandbox Code Playgroud)
谢谢
您无法使用正则表达式解析XML,因此请勿尝试.这是在Python中找到XML解析器的开始.这是一个学习在Python中解析XML的好网站.
更新:鉴于有关PyS60的新信息,这里是使用诺基亚网站XML的文档.
更新2:@Nas Banov请求了示例代码,所以这里是:
import urllib
from xml.parsers import expat
def start_element_handler(name, attrs):
"""
My handler for the event that fires when the parser sees an
opening tag in the XML.
"""
# If we care about more than just the temp data, we can extend this
# logic with ``elif``. If the XML gets really hairy, we can create a
# ``dict`` of handler functions and index it by tag name, e.g.,
# { 'humidity': humidity_handler }
if 'temp_c' == name:
print "The current temperature is %(data)s degrees Celsius." % attrs
def process_weather_conditions():
"""
Main logic of the POC; set up the parser and handle resource
cleanup.
"""
my_parser = expat.ParserCreate()
my_parser.StartElementHandler = start_element_handler
# I don't know if the S60 supports try/finally, but that's not
# the point of the POC.
try:
f = urllib.urlopen("http://www.google.com/ig/api?weather=30096")
my_parser.ParseFile(f)
finally:
f.close()
if __name__ == '__main__':
process_weather_conditions()
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2992 次 |
| 最近记录: |