【发布时间】:2018-10-27 02:14:43
【问题描述】:
对于 Python 3.5。
谁能给我一些关于在 Python 3 中使用 html5tidy 的文档?我很惊讶多次搜索没有返回任何内容。
在 Python 3 中,html5tidy.py 中的文档指出:
"""
HTML5Tidy
=========
Simple wrapper around html5lib & lxml.etree to "tidy" html in the wild to
well-formed xml/html
Usage
-----
>>> from html5tidy import tidy
>>> tidy('some text')
'<html><head/><body>some text</body></html>'
Dependencies
------------
* [html5lib](http://code.google.com/p/html5lib/)
* [lxml](http://lxml.de/)
好的,我有所有的部分:
>>> import html5lib
>>> dir(html5lib)
['HTMLParser', '__all__', '__builtins__', '__cached__', [and so on]]
>>>
>>> import lxml
>>> dir(lxml)
['__builtins__', '__cached__', '__doc__', '__file__', [and so on]]
但我注意到 dir(tidy) 只返回双下划线结果:
>>> from html5tidy import tidy
>>> dir(tidy)
['__annotations__', '__call__', '__class__', [and so on...]'__subclasshook__']
所以我以 untiidiedHTML 的形式打开一个包含 HTML 的文件。
>>> print(untidiedHTML)
<!DOCTYPE html>
<html id="ng-app" lang="en" ng-app="TH" style="" xmlns:ng="http://angularjs.org">
<head ng-controller="DZHeadController">
<meta content="text/html; charset=utf-8" http-equiv="content-type"/>
<title ng-bind="service.title">
What the Heck Is OAuth? - DZone Security
</title>
<link href="WhatIsOAuth0200_files/tranquility.css" rel="stylesheet" type="text/css"/>
</head>
<body class="tranquility" >
... and so on...
然后根据我尝试的 HTML5 整洁文档:
from html5tidy import tidy
tidiedHTML = tidy(untidiedHTML)
产生:
Traceback (most recent call last):
File "[path to my Python source file].py", line 50, in <module>
tidiedHTML = tidy(untidiedHTML)
File "/usr/local/lib/python3.5/dist-packages/html5tidy.py", line 61, in tidy
parts = [parser.parse(src, encoding=encoding, parseMeta=parseMeta, useChardet=useChardet)]
File "/usr/local/lib/python3.5/dist-packages/html5lib/html5parser.py", line 289, in parse
self._parse(stream, False, None, *args, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/html5lib/html5parser.py", line 130, in _parse
self.tokenizer = _tokenizer.HTMLTokenizer(stream, parser=self, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/html5lib/_tokenizer.py", line 36, in __init__
self.stream = HTMLInputStream(stream, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/html5lib/_inputstream.py", line 149, in HTMLInputStream
return HTMLUnicodeInputStream(source, **kwargs)
TypeError: __init__() got an unexpected keyword argument 'parseMeta'
我不知道该怎么做。我搜索了解释如何从 Python 3 调用 html5tidy 的文档,但我发现是空的......
【问题讨论】:
标签: python-3.x htmltidy