我正在嘗試使用Yahoo Weather API,但我解析了API響應的XML
的一些問題。我正在使用Python 3.4
。下面是我與工作代碼:使用ElementTree和請求進行XML解析
weather_url = 'http://weather.yahooapis.com/forecastrss?w=%s&u=%s'
url = weather_url % (zip_code, units)
try:
rss = parse(requests.get(url, stream=True).raw).getroot()
conditions = rss.find('channel/item/{%s}condition' % weather_ns)
return {
'current_condition': conditions.get('text'),
'current_temp': conditions.get('temp'),
'title': rss.findtext('channel/title')
}
except:
raise
下面是我收到堆棧跟蹤:
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "/home/jonathan/PycharmProjects/pyweather/pyweather/pyweather.py", line 42, in yahoo_conditions
rss = parse(requests.get(url, stream=True).raw).getroot()
File "/usr/lib/python3.4/xml/etree/ElementTree.py", line 1187, in parse
tree.parse(source, parser)
File "/usr/lib/python3.4/xml/etree/ElementTree.py", line 598, in parse
self._root = parser._parse_whole(source)
File "<string>", line None
xml.etree.ElementTree.ParseError: not well-formed (invalid token): line 1, column 0
的xml.etree.ElementTree解析功能不喜歡請求庫返回的原始對象。尋找到它有點深,原始對象解析爲
>>> r = requests.get('http://weather.yahooapis.com/forecastrss?w=2502265', stream=True)
>>> r.raw
<requests.packages.urllib3.response.HTTPResponse object at 0x7f32c24f9e48>
我引用this solution,但它仍然導致了同樣的問題。爲什麼上面的方法沒有工作? ElementTree.parse函數不支持urllib3響應對象嗎?我已經閱讀了所有的文檔,但他們根本沒有啓發我。
的文檔列表在這裏:
編輯: 更多的試驗後,我仍然沒有找到一個解決上面列出的問題。但是,我找到了解決方法。如果您在XML內容中使用ElementTree的fromstring方法,則一切正常。
def fetch_xml(url):
"""
Fetch a url and parse the document's XML.
:param url: the URL that the XML is located at.
:return: the root element of the XML.
:raises:
:requests.exceptions.RequestException: Requests could not open the URL.
:xml.etree.ElementTree.ParseError: xml.etree.ElementTree failed to parse the XML document.
"""
return ET.fromstring(requests.get(url).content)
我猜這種方法的缺點是它使用更多的內存。你怎麼看?我想獲得社區意見。
我有'stream = True',因爲API要求它獲取原始數據。查看文檔[here](http://docs.python-requests.org/en/latest/api/#requests.Response.raw)。我的第二個解決方案更優雅,功能更強大。感謝所提供的圖書館,但我將不得不看看! – Jonathan