在哪里可以找到Python中所有可能的HTML标记的列表?

时间:2010-03-13 18:01:04

标签: python html tags

Python中是否有标准模块列出了所有HTML标记?

例如,我想做的事情如下:

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'

要做到这一点,我需要一个Python中所有标签的列表。我想知道是否有人已将它们组装在一起或者可能包含在xml或Python库中的其他html模块中?

谢谢,Boda Cydo。

3 个答案:

答案 0 :(得分:4)

我不知道是否有内置模块可以做到这一点。我建议找一个标签列表,然后写一个像这样的函数......

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

我认为有效标记列表取决于您的doctype。这些来自http://htmldog.com/reference/htmltags/。他们说这个列表适用于严格的XHTML。

也就是说,可能有更好的方法来完成你想要做的事情。如果您想提供有关目标的更多详细信息,我相信这里的友好人士会乐意为您提供帮助。

答案 1 :(得分:2)

使用此:https://github.com/html5lib/html5lib-python/blob/master/html5lib/sanitizer.py

from html5lib.sanitizer import HTMLSanitizerMixin
print(HTMLSanitizerMixin.acceptable_elements)

答案 2 :(得分:-1)

由于python有各种模块 - smtp,xml等,问题是询问是否有像htmltags这样的模块会列出所有有效的html标签。

当您搜索http://docs.python.org网站时,您没有找到任何此类内容,是吗?

当你用Google搜索时,你发现了这个,对吧?

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

食谱366000出了什么问题?你为什么不在你的问题中提到它?你不喜欢什么?