0
從下面的html元素如何選擇,以保持文本hi there!!
和使用CSS選擇放棄其他文本Cat
丟棄來自某些要素的休息嗎?此外,使用.text
或.text.strip()
我沒有得到結果,但是當我使用.text_content()
我得到的文本。保持一定的文本,並使用選擇
from lxml.html import fromstring
html="""
<div id="item_type" data-attribute="item_type" class="ms-crm-Inline" aria-describe="item_type_c">
<div>
<label for="item_type_outer" id="Type_outer">
<div class="NotVisible">Cat</div>
Hi there!!
<div class="GradientMask"></div>
</label>
</div>
</div>
"""
root = fromstring(html)
for item in root.cssselect("#Type_outer"):
print(item.text) # doesn't work
print(item.text.strip()) # doesn't work
print(item.text_content()) # working one
結果:
Cat
Hi there!!
不過,我想獲得的結果僅僅是hi there!!
併爲我的嘗試是:
root.cssselect("#Type_outer:not(.NotVisible)") #it doesn't work either
並再次提問:
- 爲什麼
.text_content()
是工作ing但是.text
或.text.strip()
是不是? - 我怎樣才能只使用
hi there!!
CSS選擇器?
沒辦法!你非常有幫助。最後一兩件事:你能告訴我爲什麼'root.cssselect( 「#Type_outer:沒有(.NotVisible)」)'會失敗?原諒我的無知。再次感謝。 – SIM
該表達式選擇ID爲「Type_outer」的*元素沒有類「NotVisible」*,所以在這種情況下,它基本上返回與#Type_outer相同的元素,因爲具有該ID的標籤也沒有類「NotVisible」 – har07