Pyt*_*ast 2 html python jquery dom
我有一串 HTML 元素
HTMLstr = """
<div class='column span4 ui-sortable' id='column1'></div>
<div class='column span4 ui-sortable' id='column2'>
<div class='portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all' id='widget_basicLine'>
<div class='portlet-header ui-widget-header ui-corner-all'><span class='ui-icon ui-icon-minusthick'></span>Line Chart </div>
<div class='portlet-content' id=basicLine style='height:270px; margin: 0 auto;'></div>
</div>
</div>
<div class='column span4 ui-sortable' id='column3'></div> """
Run Code Online (Sandbox Code Playgroud)
我想在 python 中将上面的 HTML 字符串转换为相应的 HTML DOM 元素?
我可以通过 jQuery/AJAX 函数来完成,$(this).html(HTMLstr);但是我如何在 python 中解析它?
Python 具有用于解析 HTML 文档的内置库。在 Python 2.x 中,您可以选择HTMLParser(recommended) 和htmllib(deprecated);在 Python 3.x 中,html.parser是适当的库(这是HTMLParserPython 2.x的重命名版本)。
但是,这些是事件驱动的解析器(类似于 XML SAX 解析器),它们可能不是您想要的。如果您知道文档将是有效的 XML(即正确关闭的标签等),另一种方法是使用 Python 的 XML 解析工具之一。库xml.dom和xml.dom.minidom都是选项,具体取决于您正在寻找的解析类型(我怀疑xml.dom.minidom对于您的目的来说已经足够了,举个例子)。
例如,您应该能够在 Python 控制台中输入以下内容并获得显示的输出:
>>> import xml.dom.minidom
>>> x = xml.dom.minidom.parseString('<div class="column span4 ui-sortable" id="column2"><div class="portlet ui-widget ui-widget-content ui-helper-clearfix ui-corner-all" id="widget_basicLine" /></div>')
>>> x.documentElement.nodeName
'div'
>>> x.documentElement.getAttribute("class")
'column span4 ui-sortable'
>>> len(x.documentElement.firstChild.childNodes)
0
Run Code Online (Sandbox Code Playgroud)
此处提供了您收到的 Node 对象的完整说明。如果您习惯于在 JavaScript 中使用 DOM,您应该会发现大多数属性都是相同的。请注意,由于 Python 将其视为 XML 文档,因此诸如 'class' 之类的 HTML 特定属性没有特殊意义,因此我相信您必须使用该getAttribute函数来访问它们。
| 归档时间: |
|
| 查看次数: |
7929 次 |
| 最近记录: |