tags:

views:

119

answers:

2

Is there a standard module in Python that lists all the HTML tags?

For example, I would like to do things like:

if is_valid_html_tag('div'):
   print 'div is a valid tag'

if is_not_valid_html_tag('boda'):
   print 'boda is not a valid tag'

To do this I need a list of all tags in Python. I wonder if someone has assembled them already or maybe included in xml or some other html module in Python library?

Thanks, Boda Cydo.

+3  A: 

I don't know whether there is a built in module that does just that. I'd suggest finding a list of tags, then writing a function like this...

def is_valid_html_tag(tag_name):  
  tags=["a","abbr","acronym","address","area","b","base","bdo","big","blockquote","body","br","button","caption","cite","code","col","colgroup","dd","del","dfn","div","dl","DOCTYPE","dt","em","fieldset","form","h1","h2","h3","h4","h5","h6","head","html","hr","i","img","input","ins","kbd","label","legend","li","link","map","meta","noscript","object","ol","optgroup","option","p","param","pre","q","samp","script","select","small","span","strong","style","sub","sup","table","tbody","td","textarea","tfoot","th","thead","title","tr","tt","ul","var"]
  return tag_name in tags

I think the list of valid tags depends on your doctype. These came from http://htmldog.com/reference/htmltags/. They say this list is for Strict XHTML.

That said, there may be a better way to accomplish what you're trying to do. I'm sure the friendly folks here would be happy to help if you'd like to provide more details on your goal.

Alex JL
Thanks for the answer. I'll improve it by finding all other possible tags. The goal is just that - determine if the given string is a valid HTML tag.
bodacydo
That return statement seems more complicated than necessary. Wouldn't `return tag_name in tags` be just as good? I'd also suggest making `tags` a set rather than a list, to improve lookup speed.
Mark Dickinson
@Mark Dickinson good point, that part is convoluted for no apparent reason. Thanks for the tip about the sets too, I'm fairly new to Python and haven't used that data structure before.
Alex JL
I'd also suggest building the set ONCE, not every time the function is called.
John Machin
A: 

Since python has all kinds of modules - smtp, xml, etc, the question asks if there is a module like htmltags that would list all the valid html tags.

When you search the http://docs.python.org site, you didn't find any such thing, did you?

When you googled, you found this, right?

http://code.activestate.com/recipes/366000-htmltags-generate-html-in-python/

What's wrong with that Recipe 366000? Why didn't you mention it in your question? What didn't you like about it?

S.Lott
Why do you assume that he absolutely, without doubt, found that page through Google prior to asking the question?
Alex JL
@Code Duck: It wasn't really an "assumption". It was more of a "suggestion". It came up first for me. Folks can use Google on their own, read what's there and ask *specific* questions on SO. It's better than the "lazy web" approach of simply firing random things at SO and hoping that someone else will use Google for you.
S.Lott
@S.Lott: but you *did* search Google, doing the work for the OP. OTOH I tend to write a comment to similar questions in the spirit of “What came up on Google when you searched for [your question]?”
ΤΖΩΤΖΙΟΥ