School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become an Expert!

Join 307,149 Programmers for FREE! Get instant access to thousands of experts, tutorials, code snippets, and more! There are 1,677 people online right now. Registration is fast and FREE... Join Now!




using python to check HTML code

 

using python to check HTML code

brramiz

8 Oct, 2009 - 02:59 PM
Post #1

New D.I.C Head
*

Joined: 14 Apr, 2009
Posts: 11


My Contributions
here's what's been frying my brain for about the past 3 hours,
I need to make a program using STACKS i can't use RE's to check HTML code and see if it is valid code ie. matching tags and such.
i've got it to check if the <'s and >'s match up correctly but i can't even really think of a way to tell if the actual tags match up. for example... <b></b> i don't know how to look at those 2 so that the program can tell that they match correctly...
maybe the way i said it is a little confusing, but if you get what i'm saying or can ask any questions to clear it up and help me that would be awesome!

CODE
def htmlChecker(htmlcode):
    stack = Stack()
    stack2 = Stack()
    for ch in htmlcode:
        if ch =='<':
            if stack.isEmpty():
                stack.push(ch)
            else:
                return stack.isEmpty()
        if ch =='>':
            stack.pop()
        if not stack.isEmpty()and ch!='<':
            stack2.push(ch)
    print stack2


Building stacks out of this:
CODE

class Stack():
    """Top of the stack is at the end of the list"""
    def __init__(self):
        self._items = []

    def push(self,obj):
        self._items.append(obj)

    def pop(self):
        return self._items.pop()

    def peek(self):
        return self._items[-1]

    def isEmpty(self):
        return len(self._items)==0

    def __len__(self):
        return len(self._items)

    def __str__(self):
        return "bottom "+str(self._items)+ " top"

    def reverse(self):
        return self._items.reverse()

    def lower(self):
        return self._items.lower()


User is offlineProfile CardPM
+Quote Post


programble

RE: Using Python To Check HTML Code

12 Oct, 2009 - 02:15 PM
Post #2

D.I.C Regular
Group Icon

Joined: 21 Feb, 2009
Posts: 423



Thanked: 10 times
Dream Kudos: 50
My Contributions
You should be pushing when you encounter a <x> and popping when you have a </x>. If there is anything left on the stack at the end, the HTML is not valid.Since you can't use regex, then you can resort to simple string splitting. I would suggest splitting at ">". To verify that they match up, you need to check that the object you are popping matches the one you have encountered. If it does not, the HTML is not valid.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 04:28PM

Live Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month