python lxml 使用

Posted RoyFans

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了python lxml 使用相关的知识,希望对你有一定的参考价值。

>>> import lxml.html
>>> broken_html=‘<ul class=country><li>Area<li>Population</ul>‘
>>> tree=lxml.html.fromstring(broken_html) #parse the HTML
>>> fixed_html=lxml.html.tostring(tree,pretty_print=True)
>>> print fixed_html
<ul class="country">
<li>Area</li>
<li>Population</li>
</ul>

>>> li=tree.cssselect(‘ul.country > li‘)[0]
>>> area=li.text_content()
>>> print area
Area
>>> 

  

以上是关于python lxml 使用的主要内容,如果未能解决你的问题,请参考以下文章