xpath之string(.)方法
生活随笔
收集整理的這篇文章主要介紹了
xpath之string(.)方法
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
from lxml import etreehtml = '''<li class="tag_1">需要的內容1<a>需要的內容2</a></li>
'''selector = etree.HTML(html )
contents = selector.xpath ( '//li[@class = "tag_1"]')
contents1 = selector.xpath ( '//li[@class = "tag_1"]')[0]
contents2 = contents1.xpath('string(.)')
contents3 = selector.xpath ( '//li[@class = "tag_1"]/text()')
print(contents) #[<Element li at 0x2c55e88>]
print(contents1) # <Element li at 0x2c55e88>
print(contents2)
print(contents3)
輸出
D:\Python\venv\Scripts\python.exe D:/Python/venv/test9.py [<Element li at 0x2c75ec8>] <Element li at 0x2c75ec8> 需要的內容1需要的內容2['需要的內容1\n ', '\n ']Process finished with exit code 0string(.)可以用于提取標簽嵌套標簽的內容。
轉載于:https://blog.51cto.com/12884584/2352683
總結
以上是生活随笔為你收集整理的xpath之string(.)方法的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 为什么总梦到男朋友和别的女人在一起
- 下一篇: 深入bind