import xml.sax import io # A sample of HTML to test html = u'<ul class="class"><li>Lorem ipsum dolor sit amet, consectetuer adipiscing elit.</li><li class="class">Aliquam tincidunt mauris eu risus.</li><li>Vestibulum auctor dapibus neque.</li></ul>' class TagCounter(xml.sax.ContentHandler): """A simple SAX Handler counting XML tags""" def __init__(self, listener): """ The listener parameter should be a function that has 2 parameters: - name: the name of the most occurent tag - count: the number of tag found It will be trigger to notify when the parsing is done """ self.listener = listener self.tags = dict() def startElement(self, name, attrs): """Counting elements according to there name""" try: self.tags[name] += 1 except KeyError: self.tags[name] = 1 def endDocument(self): """Looking for the most common and notifying it's done""" max = 0 name = '' for tag, cpt in self.tags.iteritems(): if tag > max: max = cpt name = tag self.listener(name, max) # Defining the listning function def tagCounterListener(name, count): print "Most common tag: " + str(name) + " with " + str(count) + " instances" # Starting to parse parser = xml.sax.make_parser() parser.setContentHandler(TagCounter(tagCounterListener)) parser.parse(io.StringIO(html))
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question