Jusrock
Jusrock

Reputation: 21

How to read html tags using beautifulsoup

I am trying to read html tag using beautifulsoap and checking whether some tags are available or missing.

I am reading the file using beautifulsoup and then using it in my test files.

Here what i tried but didn't get it to work:

class Testing(unittest.TestCase):
        @classmethod
        def setUp(name):

            name.html = None
            with open("index.html") as frd:
                name.html = frd.read()
                name.soup = BeautifulSoup(name.html)
            if not name.html:
                raise Exception('cant read')    

        def testing(self)
         assert self.soup.find('html') == 'html'
          #Raise : error

I can't find html tag using find() function in soup (tried printing it to see the output but dint work). How can I raise an exception if HTML tag is missing in a html file?

Upvotes: 0

Views: 450

Answers (1)

Sharif Mamun
Sharif Mamun

Reputation: 3574

Try this as when you are using find it returns the beautified strings or None! So, this thing I can suggest!

try:
    assert self.soup.find('html') != None
except AssertionError, e:
    raise Exception("HTML Tag is missing!")

Upvotes: 1

Related Questions