1. 程式人生 > 其它 >xpath選擇器使用

xpath選擇器使用

# xpath教程
# https://www.w3school.com.cn/xpath/index.asp
    

# xpath: XPath 是一門在 XML 文件中查詢資訊的語言
# / :從根節點選取。
# // :不管位置,直接找
# /@屬性名
# /text()
# 會複製()

doc='''
<html>
 <head>
  <base href='http://example.com/' />
  <title>Example website</title>
 </head>
 <body>
  <div id='images'>
   <a href='image1.html' aa='bb'>Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
   <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
   <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
   <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
   <a href='image5.html' class='li li-item' name='items'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
   <a href='image6.html' name='items'><span><h5>test</h5></span>Name: My image 6 <br /><img src='image6_thumb.jpg' /></a>
  </div>
 </body>
</html>
''' from lxml import etree html=etree.HTML(doc) # html=etree.parse('search.html',etree.HTMLParser()) # 1 所有節點 # a=html.xpath('//*') # 2 指定節點(結果為列表) # a=html.xpath('//head') # 3 子節點,子孫節點 # a=html.xpath('//div/a') # a=html.xpath('//body/a') #無資料 # a=html.xpath('//body//a') # 4 父節點 # a=html.xpath('//body//a[@href="image1.html"]/..')
# a=html.xpath('//body//a[1]/..') # 也可以這樣 # a=html.xpath('//body//a[1]/parent::*') # 5 屬性匹配 # a=html.xpath('//body//a[@href="image1.html"]') # 6 文字獲取(重要) /text() 取當前標籤的文字 # a=html.xpath('//body//a[@href="image1.html"]/text()') # a=html.xpath('//body//a/text()') # 7 屬性獲取 @href 取當前標籤的屬性 # a=html.xpath('//body//a/@href') #
# 注意從1 開始取(不是從0) # a=html.xpath('//body//a[1]/@href') # 8 屬性多值匹配 # a 標籤有多個class類,直接匹配就不可以了,需要用contains # a=html.xpath('//body//a[@class="li"]') # a=html.xpath('//body//a[contains(@class,"li")]') # a=html.xpath('//body//a[contains(@class,"li")]/text()') # 9 多屬性匹配 # a=html.xpath('//body//a[contains(@class,"li") or @name="items"]') # a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()') # a=html.xpath('//body//a[contains(@class,"li")]/text()') # 10 按序選擇 # a=html.xpath('//a[2]/text()') # a=html.xpath('//a[2]/@href') # 取最後一個 # a=html.xpath('//a[last()]/@href') # 位置小於3的 # a=html.xpath('//a[position()<3]/@href') # 倒數第二個 # a=html.xpath('//a[last()-2]/@href') # 11 節點軸選擇 # ancestor:祖先節點 # 使用了* 獲取所有祖先節點 # a=html.xpath('//a/ancestor::*') # # 獲取祖先節點中的div # a=html.xpath('//a/ancestor::div') # attribute:屬性值 # a=html.xpath('//a[1]/attribute::*') # a=html.xpath('//a[1]/@aa') # child:直接子節點 # a=html.xpath('//a[1]/child::*') # a=html.xpath('//a[1]/child::img/@src') # descendant:所有子孫節點 # a=html.xpath('//a[6]/descendant::*') # a=html.xpath('//a[6]/descendant::h5/text()') # following:當前節點之後所有節點(兄弟節點和兄弟內部的節點) # a=html.xpath('//a[1]/following::*') # a=html.xpath('//a[1]/following::*[1]/@href') # following-sibling:當前節點之後同級節點(只找兄弟) # a=html.xpath('//a[1]/following-sibling::*') # a=html.xpath('//a[1]/following-sibling::a') # a=html.xpath('//a[1]/following-sibling::*[2]') # a=html.xpath('//a[1]/following-sibling::*[2]/@href') print(a) # / # // # /@屬性名 # /text() //以後去查詢標籤,bs4的find, css,xpath(通用的)