cdl*_*ary 31 html python validation xhtml
什么是验证文档是否遵循某些HTML版本(最好是我可以指定)的最佳方法?我希望能够知道故障发生的位置,就像在基于Web的验证器中一样,除了在本机Python应用程序中.
小智 24
PyTidyLib是HTML Tidy的一个很好的python绑定.他们的例子:
from tidylib import tidy_document
document, errors = tidy_document('''<p>fõo <img src="bar.jpg">''',
options={'numeric-entities':1})
print document
print errors
Run Code Online (Sandbox Code Playgroud)
此外,它兼容遗留的HTML Tidy和新的tidy-html5.
Mar*_*epp 17
我认为它是最优雅的方式来调用W3C验证服务
http://validator.w3.org/
Run Code Online (Sandbox Code Playgroud)
编程.很少有人知道你不必屏幕抓取结果才能得到结果,因为服务返回非标准的HTTP标头参数
X-W3C-Validator-Recursion: 1
X-W3C-Validator-Status: Invalid (or Valid)
X-W3C-Validator-Errors: 6
X-W3C-Validator-Warnings: 0
Run Code Online (Sandbox Code Playgroud)
用于指示错误和警告的有效性和数量.
例如,命令行
curl -I "http://validator.w3.org/check?uri=http%3A%2F%2Fwww.stalsoft.com"
Run Code Online (Sandbox Code Playgroud)
回报
HTTP/1.1 200 OK
Date: Wed, 09 May 2012 15:23:58 GMT
Server: Apache/2.2.9 (Debian) mod_python/3.3.1 Python/2.5.2
Content-Language: en
X-W3C-Validator-Recursion: 1
X-W3C-Validator-Status: Invalid
X-W3C-Validator-Errors: 6
X-W3C-Validator-Warnings: 0
Content-Type: text/html; charset=UTF-8
Vary: Accept-Encoding
Connection: close
Run Code Online (Sandbox Code Playgroud)
因此,您可以优雅地调用W3C验证服务并从HTTP标头中提取结果:
# Programmatic XHTML Validations in Python
# Martin Hepp and Alex Stolz
# mhepp@computer.org / alex.stolz@ebusiness-unibw.org
import urllib
import urllib2
URL = "http://validator.w3.org/check?uri=%s"
SITE_URL = "http://www.heppnetz.de"
# pattern for HEAD request taken from
# http://stackoverflow.com/questions/4421170/python-head-request-with-urllib2
request = urllib2.Request(URL % urllib.quote(SITE_URL))
request.get_method = lambda : 'HEAD'
response = urllib2.urlopen(request)
valid = response.info().getheader('X-W3C-Validator-Status')
if valid == "Valid":
valid = True
else:
valid = False
errors = int(response.info().getheader('X-W3C-Validator-Errors'))
warnings = int(response.info().getheader('X-W3C-Validator-Warnings'))
print "Valid markup: %s (Errors: %i, Warnings: %i) " % (valid, errors, warnings)
Run Code Online (Sandbox Code Playgroud)
kar*_*cow 11
您可以决定在本地安装HTML验证程序并创建客户端以请求验证.
在这里,我创建了一个程序来验证txt文件中的url列表.我只是检查HEAD以获得验证状态,但是如果你进行GET,你将获得完整的结果.查看验证器的API,有很多选项.
import httplib2
import time
h = httplib2.Http(".cache")
f = open("urllistfile.txt", "r")
urllist = f.readlines()
f.close()
for url in urllist:
# wait 10 seconds before the next request - be nice with the validator
time.sleep(10)
resp= {}
url = url.strip()
urlrequest = "http://qa-dev.w3.org/wmvs/HEAD/check?doctype=HTML5&uri="+url
try:
resp, content = h.request(urlrequest, "HEAD")
if resp['x-w3c-validator-status'] == "Abort":
print url, "FAIL"
else:
print url, resp['x-w3c-validator-status'], resp['x-w3c-validator-errors'], resp['x-w3c-validator-warnings']
except:
pass
Run Code Online (Sandbox Code Playgroud)
html5lib模块可用于验证 HTML5 文档:
>>> import html5lib
>>> html5parser = html5lib.HTMLParser(strict=True)
>>> html5parser.parse('<html></html>')
Traceback (most recent call last):
...
html5lib.html5parser.ParseError: Unexpected start tag (html). Expected DOCTYPE.
Run Code Online (Sandbox Code Playgroud)
试试tidylib.您可以获得一些非常基本的绑定作为elementtidy模块的一部分(从HTML文档构建元素树).http://effbot.org/downloads/#elementtidy
>>> import _elementtidy
>>> xhtml, log = _elementtidy.fixup("<html></html>")
>>> print log
line 1 column 1 - Warning: missing <!DOCTYPE> declaration
line 1 column 7 - Warning: discarding unexpected </html>
line 1 column 14 - Warning: inserting missing 'title' element
Run Code Online (Sandbox Code Playgroud)
解析日志应该可以为您提供所需的一切.