Validate(X)HTML Python
-
09-06-2019 - |
質問
どの道のり約の妥当性を検証する書面を以下にあ版HTML(prefereablyることを指定できま)?私は知ることができるようになるの故障は、ウェブサイトvalidatorを除き、ネイティブのPythonアプリです。
他のヒント
PyTidyLib 素敵なpython binding HTMLません。その例:
from tidylib import tidy_document
document, errors = tidy_document('''<p>fõo <img src="bar.jpg">''',
options={'numeric-entities':1})
print document
print errors
また両方で使用できます レガシィHTMLテ の 新しいテ-html5.
と思い、この呼び出しは、W3Cの検証サービス
http://validator.w3.org/
プログラム.ほとんど知られていないないことをしている画面を掻きの結果を得るためにその結果、サービスを返します非標準のHTTPヘッダのパラメータ
X-W3C-Validator-Recursion: 1
X-W3C-Validator-Status: Invalid (or Valid)
X-W3C-Validator-Errors: 6
X-W3C-Validator-Warnings: 0
ことを示すの妥当性とエラー数の下方修正を発表している。
たとえば、コマンドライン
curl -I "http://validator.w3.org/check?uri=http%3A%2F%2Fwww.stalsoft.com"
を返します
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
することで、優雅に呼び出しは、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)
決めができますインストールのHTML validator地域をクライアントのバリデーションを実施します。
ここにいたので実際に使ったプログラムを検証するurlのリストを、txtファイルです。どうしてそうなっちゃうんですかチェック本社の検証状況だいだいにあるセレクトショップ。の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
みtidylib.きのある基本的なバインディングの一環としてelementtidyモジュール(ビルドelementtreesから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
構文解析をログすべきだなど様々な機能が詰まっています。
ると思い HTMLテ いと考えている。あPython bindingます。
私の場合は、python W3C/HTML検証パッケージだったのか pip search w3c
(月-2016年度)
いること
$ pip install requests
$ python
Python 2.7.12 (default, Jun 29 2016, 12:46:54)
[GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> r = requests.post('https://validator.w3.org/nu/',
... data=file('index.html', 'rb').read(),
... params={'out': 'json'},
... headers={'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/41.0.2272.101 Safari/537.36',
... 'Content-Type': 'text/html; charset=UTF-8'})
>>> r.text
>>> u'{"messages":[{"type":"info", ...
>>> r.json()
>>> {u'messages': [{u'lastColumn': 59, ...
より文書をこちら pythonのご要望, W3CのAPI Validator
これは非常に基本的なhtmlのバリデータに基づくlxmlのHTMLParser.る必要はありませんインターネット接続します。
_html_parser = None
def validate_html(html):
global _html_parser
from lxml import etree
from StringIO import StringIO
if not _html_parser:
_html_parser = etree.HTMLParser(recover = False)
return etree.parse(StringIO(html), _html_parser)
ことに注意して確認することはありません閉じタグで、例えば、以下の通り:
validate_html("<a href='example.com'>foo</a>")
ただし、以下のポー:
validate_html("<a href='example.com'>foo</a")