xpath获取带注释的text

时间:2021-11-22 14:42:58

from lxml import etree

html_str = """
<div id="box1">this from blog.csdn.net/lncxydjq , DO NOT COPY!
<div id="box2">*****
<!--can u get me, bitch?-->
</div>
</div>
"""

html = etree.HTML(html_str)

print html.xpath('//div[@id="box1"]/div/node()')[1]
print type(html.xpath('//div[@id="box1"]/div/node()')[1])
print html.xpath('//div[@id="box1"]/div/node()')[1].text

"""output:
<!--can u get me, bitch?-->
<type 'lxml.etree._Comment'>
can u get me, bitch?

参考:https://blog.csdn.net/lncxydjq/article/details/77880824