python xpath基础 03

from lxml import etree

text = '''
<div>
    <ul>
         <li class="item-0"><a href="link1.html">first item</a></li>
         <li class="item-1"><a href="link2.html">second item</a></li>
         <li class="item-inactive"><a href="link3.html">third item</a></li>
         <li class="item-1"><a href="link4.html">fourth item</a></li>
         <li class="item-0"><a href="link5.html">fifth item</a>
     </ul>
 </div>
'''
html = etree.HTML(text)
result = html.xpath('//li[1]/a/text()')
print(result)#输出第一个超链接里面的内容
result = html.xpath('//li[last()]/a/text()')
print(result)#输出最后一个超链接的内容
result = html.xpath('//li[position()<3]/a/text()')
print(result)#输出位置小于3的a标签里面的内容
result = html.xpath('//li[last()-2]/a/text()')
print(result)#输出最后一个链接向前移动两个位置的a标签内容
'''

输出结果:
['first item']
['fifth item']
['first item', 'second item']
['third item']

'''

 

posted @ 2018-12-25 20:34  青春叛逆者  阅读(159)  评论(0编辑  收藏  举报