這裏有一種方法,你可以得到使用使用lxml數據:
import urllib2
import lxml.etree
url = "http://weather.yahooapis.com/forecastrss?w=24260013&u=c"
doc = lxml.etree.parse(urllib2.urlopen(url)).getroot()
conditions = doc.xpath('*/*/yweather:condition',
namespaces={'yweather': 'http://xml.weather.yahoo.com/ns/rss/1.0'})
try:
condition=conditions[0]
except IndexError:
print('yweather:condition not found')
print(condition.items())
# [('text', 'Fair'), ('code', '33'), ('temp', '16'), ('date', 'Wed, 19 May 2010 9:55 pm EDT')]
上using xpath with namespaces的部分可能是特別有幫助。