另外我們還可能遇到一種情況,那就是根據(jù)多個(gè)屬性確定一個(gè)節(jié)點(diǎn),這時(shí)就需要同時(shí)匹配多個(gè)屬性,此時(shí)可用運(yùn)用and運(yùn)算符來連接使用:
from lxml import etree
text1='''
<div>
<ul>
<li class="aaa" name="item"><a href="link1.html">第一個(gè)</a></li>
<li class="aaa" name="fore"><a href="link2.html">second item</a></li>
</ul>
</div>
'''
html=etree.HTML(text1,etree.HTMLParser())
result=html.xpath('//li[@class="aaa" and @name="fore"]/a/text()')
result1=html.xpath('//li[contains(@class,"aaa") and @name="fore"]/a/text()')
print(result)
print(result1)
#
['second item']
['second item']
更多建議: