From: W. Trevor King Date: Wed, 29 Feb 2012 22:31:08 +0000 (-0500) Subject: Update YahooGrabber in ticker.py. X-Git-Url: http://git.tremily.us/?a=commitdiff_plain;h=bec2fdf2046fde6ec5e996ac3decc20fecfdddd4;p=blog.git Update YahooGrabber in ticker.py. --- diff --git a/posts/ticker/ticker.py b/posts/ticker/ticker.py index f96aadd..4712089 100755 --- a/posts/ticker/ticker.py +++ b/posts/ticker/ticker.py @@ -1,5 +1,5 @@ #!/usr/bin/env python -# Copyright (C) 2010 W. Trevor King +# Copyright (C) 2010-2012 W. Trevor King # # 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 @@ -118,36 +118,17 @@ class YahooGrabber (Grabber): def _parse_html(self, html): """Extract quote from a snippet that looks like:: - - Last Trade: - - - - - 64.74 - - - - - - - For the implementation, see the `LXML tutorial`_. - - .. _LXML tutorial: - http://codespeak.net/lxml/tutorial.html#using-xpath-to-find-text + + + 67.62 + + """ 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)