#!/usr/bin/env python
-# Copyright (C) 2010 W. Trevor King <wking@drexel.edu>
+# Copyright (C) 2010-2012 W. Trevor King <wking@drexel.edu>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
def _parse_html(self, html):
"""Extract quote from a snippet that looks like::
- <tr>
- <th ...>Last Trade:</th>
- <td ...>
- <big>
- <b>
- <span ...>
- 64.74
- </span>
- </b>
- </big>
- </td>
- </tr>
-
- For the implementation, see the `LXML tutorial`_.
-
- .. _LXML tutorial:
- http://codespeak.net/lxml/tutorial.html#using-xpath-to-find-text
+ <span class="time_rtq_ticker">
+ <span>
+ 67.62
+ </span>
+ </span>
"""
parser = etree.HTMLParser()
tree = etree.parse(StringIO(html), parser)
root = tree.getroot()
- rows = root.xpath('.//tr') #[[td/text() = 'Last Trade:']")
- for row in rows:
- has_label = row.xpath(".//th/text() = 'Last Trade:'")
- if has_label:
- break
- assert has_label, '\n---\n\n'.join([
- etree.tostring(row, pretty_print=True) for row in rows])
- data = row.xpath('.//td')[0]
- text = ''.join(data.itertext()).strip()
+ span = root.xpath(".//span[@class='time_rtq_ticker']")[0]
+ text = ''.join(span.itertext()).strip()
return float(text)