首页 > 解决方案 > 无法弄清楚如何从 Python 3 调用 html5Tidy

问题描述

对于 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__']

所以我打开一个包含 HTML 为untiiedHTML的文件。

>>> 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.xhtmltidy

解决方案


该库已损坏和/或不适用于 python 3.5。我安装并遇到了与 html5lib.HTMLParser https://github.com/aleray/html5tidy/blob/master/html5tidy.py#L57相关的错误

有一个贡献者,该软件包已 6 年未更新

你的选择是

  • 分叉回购,解决问题并提交拉取请求
  • 提取您需要的代码并自己滚动
  • 寻找另一个图书馆

推荐阅读