from lxml.html import tostring
from lxml import etree
res_news = etree.HTML(aa)
# 获取某标签下的html源码(标签+内容)
content = res_news.xpath('//*[@id="main"]')[0]
html = tostring(content, encoding="utf-8").decode("utf-8") #编码格式为源码编码格式
print(html)
# 注: tostring()方法即可把通过xpath定位到的标签(含该标签)及其下的所有标签输出,
切记使用decode()方法来进行解码
网友评论