Get parts of html code as a new string in python
Question: I was wondering how I could get a value, between some html tags, from some
html code using python.

Say I wanted to get the price of a product in an amazon page:

I've got up to:

    
    
    url = raw_input("Enter the url:\n")
    sock = urllib.urlopen(url)
    htmlsource = sock.read()
    sock.close()
    

so now I got the html source as a string but I don't know how to extract the
price. I've played around with re.search but can't get the right expression.

say the price is between `<span class="price">£79.98</span>`

What would be the best way to get `var1 = 79.98`?


Answer: You need to use a HTML Parsing Library. It provides better features than using
standard regexs, where you can go wrong easily and it is hard to maintain.
Python Standard Library comes with `html.parse` in py3k and `HTMLParser` in
python2.x series which would help you parse the HTML file and get the values
of the tags.

You may also use
[BeautifulSoup](http://www.crummy.com/software/BeautifulSoup/) library which
many have found easy to use.

    
    
    from BeautifulSoup import BeautifulSoup
    soup = BeautifulSoup('<span class="price">79.98</span>')
    t = soup.find('span', attrs={"class":"price"})
    print t.renderContents()
    

