代码:
text = '''
<div id="cont">
<ul class="slist">
<li class="item-0">web开发</li>
<li class="item-1"><a href="link2.html">爬虫开发</a></li>
<li class="item-0 active"><a href="link3.html"><span class="bold">数据分析</span></a></li>
<li class="item-1 active"><a href="link4.html">深度学习</a></li>
<li class="item-0"><a href="link5.html">机器学习</a>
</ul>
'''
from lxml import etree
html = etree.HTML(text)
result = html.xpath('//a')[2]
print(etree.tostring(result, encoding='utf-8', method='html').decode('utf-8'))
为什么输出结果是这么一大坨的?
<a href="link4.html">深度学习</a></li>
<li class="item-0"><a href="link5.html">机器学习</a>
</li></ul>
</div></body></html>
我已经在网上连续试了好几个代码了,直接copy过来测试,但是输出结果都很奇怪(总是会把后面的html也选取了),与网上给出的结果都不一样。