Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting html tag value in python

I'm newbie to python. Here is my code working on python 2.7.5

import urllib2
import sys       

url ="mydomain.com"
usock = urllib2.urlopen(url)
data = usock.read()
usock.close()

print data

Getting HTML markup like that and it works.

What I want to do is, to get value from inside <font class="big"></font> tag. for ex. I need data value from this example:

<font class="big">Data</font>

How to do it?

like image 451
heron Avatar asked Sep 06 '13 11:09

heron


1 Answers

You can use a HTML parser module such as BeautifulSoup:

from bs4 import BeautifulSoup as BS
url ="mydomain.com"
usock = urllib2.urlopen(url)
data = usock.read()
usock.close()
soup = BS(data)
print soup.find('font', {'class':'big'}).text

This finds a tag <font> with a class="big". It then prints its content.

like image 179
TerryA Avatar answered Sep 28 '22 18:09

TerryA