python检查html有效 [英] python check html valid

查看:103
本文介绍了python检查html有效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何使用python检查html代码的有效性? 我需要关闭标签检查,并在标签参数中使用大括号.例如| a href ="xxx'|以及其他可能的验证,我可以使用这些文件来实现此目的?

How can i check valid of html code with python? i need closed tags check, and braces in tags params. Such as |a href="xxx'| and other possible validations, wich libs i can use for this?

推荐答案

好吧,这并不是您要找的东西,但是要验证我工作的网站的HTML,我请W3C验证程序验证为我检查一下,然后我将屏幕抓取输出以得到基本的是/否结果.请注意,网络上有多种验证服务可以替代,但是W3C对我来说足够好用.

Well, this isn't exactly what you're looking for, but to validate the HTML for a web site I work on, I ask the W3C Validator to check it for me, and I just screen scrape the output to get the basic yes/no result. Note there are several validation services on the web as alternatives, but W3C works well enough for me.

#!/usr/bin/python2.6
import re
import urllib
import urllib2

def validate(URL):
    validatorURL = "http://validator.w3.org/check?uri=" + \
        urllib.quote_plus(URL)
    opener = urllib2.urlopen(validatorURL)
    output = opener.read()
    opener.close()
    if re.search("This document was successfully checked as".replace(
            " ", r"\s+"), output):
        print "    VALID: ", URL
    else:
        print "INVALID: ", URL

这篇关于python检查html有效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆