# 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>' # Function wich count the most reccurent tag in an HTML input def tagCounter(input): tags = dict() opened = False name = '' # For each char in the input for char in input: # Making sure the attributes won't get in the way if char == ' ' and opened: opened = False # Detecting the end of a tag elif char == '>': if name[0] != '/': try: tags[name] += 1 except KeyError: tags[name] = 1 name = '' opened = False # Detecting the begenning of a tag elif char == '<': opened = True continue # If opened then we are currently parsing the name of a tag if opened: name += char # Looking for the most common tag and printing it max = 0 for tag, cpt in tags.iteritems(): if tag > max: max = cpt name = tag print "Most common tag: " + str(name) + " with " + str(max) + " instances" # Let's try tagCounter(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