xpath获取带注释的text


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

猜你喜欢

转载自www.cnblogs.com/hankleo/p/10450019.html