【问题标题】:Can't figure out how to invoke html5Tidy from Python 3无法弄清楚如何从 Python 3 调用 html5Tidy
【发布时间】: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


    【解决方案1】:

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

    只有一个贡献者,包已经 6 年没有更新了

    你的选择是

    • fork 存储库,修复问题并提交拉取请求
    • 提取您需要的代码并自己滚动
    • 查找另一个库

    【讨论】:

    • 哇——谢谢你!令我惊讶的是,许多 Web 搜索都没有返回结果。您已经解释了为什么会这样。
    • 很高兴能帮上忙!
    猜你喜欢
    • 1970-01-01
    • 2014-12-18
    • 1970-01-01
    • 1970-01-01
    • 2019-12-11
    • 2013-05-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多