Created
March 5, 2012 09:15
-
-
Save jirwin/1977593 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from lxml.html import parse | |
# this can be a full weburl as well | |
doc = parse('knives.html').getroot() | |
knives = {} | |
# start by iterating through every table cell | |
for td in doc.cssselect('td'): | |
# If the cell has align=center, valign=top, and isn't blank | |
if (td.get('align') == 'center' and td.get('valign') == 'top' and | |
td.text_content().strip()): | |
# This is the name of the knife | |
name = td.cssselect('b')[0].text_content().strip() | |
# descend further to find the price | |
for new_td in td.cssselect('div table tbody tr td'): | |
price = new_td.text_content().strip() | |
if price.startswith('$'): | |
# knives[name] = price[1:] <--- No dollar sign | |
knives[name] = price | |
# Print the values we saved | |
for name, price in knives.iteritems(): | |
print "%s, %s" % (name, price) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment