Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import re
- from xml.sax.saxutils import unescape
- html_str = """\
- <div class="productdetailcontainer" id="detailtab1"><div class="r nom"><div class="c10"><div class="space ptb"><b>Description</b><br />Très confortable pour un sommeil réparateur. Idéal pour soulager les tensions : revêtement en mousse confortable et 7 zones de confort constituées de 600 ressorts ensachés pour un soutien ciblé et optimal de l'ensemble du corps. Position de couchage surélevée : hauteur totale du matelas : env. 19 cm. Régulation optimale de la température : la housse surpiquée de fibres respirantes de chaque côté et les poches d'air dans les ressorts ensachés permettent une aération parfaite du matelas. Facile à transporter et rapide à déballer. Se déplace et se retourne facilement grâce aux 4 poignées.<p fontsize:1.0em="" style=""><strong>Vous hésitez dans votre choix du meilleur matelas? Cliquez <a advies="" frbe="" href="https://www.lidlshop.be/frBE/wonen/slaapkamer/matrassen/advies" https:="" matrassen="" slaapkamer="" wonen="" www.lidlshop.be="">ici</a> pour obtenir plus d'infos.</strong></p></div></div><div><ul class="attributes" id="mattresslist"> <li class="r nom"><strong class="c2 space pr">Noyau</strong><span class="c8">Ressorts ensachés</span></li> <li class="r nom"><strong class="c2 space pr">Housse</strong><span class="c8">100% polyester</span></li> <li class="r nom"><strong class="c2 space pr">Mesures</strong><span class="c8">140 x 200 cm</span></li> <li class="r nom"><strong class="c2 space pr">Hauteur</strong><span class="c8">19 cm</span></li> <li class="r nom"><strong class="c2 space pr">Poids</strong><span class="c8">Convient pour un poids corporel de 80100 kg</span></li> <li class="r nom"><strong class="c2 space pr">Zones de confort</strong><span class="c8">7</span></li> <li class="r nom"><strong class="c2 space pr">Degré de fermeté</strong><span class="c8">H3</span></li> <li class="r nom"><strong class="c2 space pr">Antiallergique</strong><span class="c8">Oui</span></li> <li class="r nom"><strong class="c2 space pr">Housse amovible</strong><span class="c8">Oui</span></li> <li class="r nom"><strong class="c2 space pr">Fermeture à glissière</strong><span class="c8">Fermeture zippée sur les 4 côtés</span></li> <li class="r nom"><strong class="c2 space pr">Instructions d'entretien</strong><span class="c8">Lavable en machine à 60 °C sur le programme linge délicat</span></li> <li class="r nom"><strong class="c2 space pr">Labels de qualité</strong><span class="c8">5</span></li></ul></div></div></div>"""
- def check_html(html):
- def remove_space_in_closing_tags(html):
- thtml = unescape(html.replace("'","\"")).replace("
","")
- while "</ " in thtml:
- thtml = thtml.replace("</ ","</")
- return thtml
- def build_tags(html):
- closing_tag_list = []
- opening_tag_list = []
- # ultimate_regexp = "(?i)<\/?\w+((\s+\w+(\s*=\s*(?:\".*?\"|'.*?'|[^'\">\s]+))?)+\s*|\s*)\/?>" # used to extract htlm tags from html
- ultimate_regexp = "<.+?>"
- for match in re.finditer(ultimate_regexp, html):
- try:
- tag = repr(match.group()).split(" ")[0]
- except:
- tag = repr(match.group())
- tag = tag.replace("<","").replace(">","").replace("'","")
- if tag.startswith("/"):
- closing_tag_list.append(tag.lower())
- else:
- opening_tag_list.append(tag.lower())
- return opening_tag_list, closing_tag_list
- def compare_tags(opening_tag_list, closing_tag_list):
- errors = []
- self_closing = ['area', 'base', 'br', 'col', 'embed', 'hr', 'hr/', 'img','input', 'link', 'meta', 'param', 'source', 'track', 'wbr', 'bgsound']
- unique_opening_tag_list = list(dict.fromkeys(opening_tag_list))
- unique_closing_tag_list = list(dict.fromkeys(closing_tag_list))
- for tag in unique_opening_tag_list:
- opening_tags_count = opening_tag_list.count(tag)
- closing_tags_couunt = closing_tag_list.count("/" + tag)
- if opening_tags_count == closing_tags_couunt or tag in self_closing or '!' in tag or ":" in tag:
- pass
- else:
- errors.append("tag {} - KO - {} opening tags and {} closing tags".format(tag, opening_tags_count, closing_tags_couunt))
- for tag in unique_closing_tag_list:
- opening_tags_count = opening_tag_list.count(tag[1:])
- closing_tags_couunt = closing_tag_list.count(tag)
- if opening_tags_count == closing_tags_couunt or tag in self_closing or tag[1:] in unique_opening_tag_list or '!' in tag or ":" in tag:
- pass
- else:
- errors.append("tag {} - KO - {} opening tags and {} closing tags".format(tag, opening_tags_count, closing_tags_couunt))
- return errors
- opening_tags, closing_tags = build_tags(remove_space_in_closing_tags(html))
- return compare_tags(opening_tags, closing_tags)
- if __name__ == '__main__':
- result = check_html(html_str)
- if result :
- print(result)
- else:
- print("OK")
Add Comment
Please, Sign In to add comment