1、簡介html
XPath 是一門在 XML 文檔中查找信息的語言。XPath 可用來在 XML 文檔中對元素和屬性進行遍歷。XPath 是 W3C XSLT 標準的主要元素,而且 XQuery 和 XPointer 都構建於 XPath 表達之上。python
參照scrapy
2、安裝ide
pip3 install lxml
3、使用ui
一、導入url
from lxml import etree
二、基本使用spa
from lxml import etree wb_data = """ <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(wb_data) print(html) result = etree.tostring(html) print(result.decode("utf-8"))
從下面的結果來看,咱們打印機html其實就是一個python對象,etree.tostring(html)則是不全裏html的基本寫法,補全了缺胳膊少腿的標籤。code
<Element html at 0x39e58f0> <html><body><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> </li></ul> </div> </body></html>
三、獲取某個標籤的內容(基本使用),注意,獲取a標籤的全部內容,a後面就不用再加正斜槓,不然報錯。xml
寫法一htm
html = etree.HTML(wb_data) html_data = html.xpath('/html/body/div/ul/li/a') print(html) for i in html_data: print(i.text) <Element html at 0x12fe4b8> first item second item third item fourth item fifth item
寫法二(直接在須要查找內容的標籤後面加一個/text()就行)
html = etree.HTML(wb_data) html_data = html.xpath('/html/body/div/ul/li/a/text()') print(html) for i in html_data: print(i) <Element html at 0x138e4b8> first item second item third item fourth item fifth item
四、打開讀取html文件
#使用parse打開html的文件 html = etree.parse('test.html') html_data = html.xpath('//*')
#打印是一個列表,須要遍歷 print(html_data) for i in html_data: print(i.text)
html = etree.parse('test.html') html_data = etree.tostring(html,pretty_print=True) res = html_data.decode('utf-8') print(res) 打印: <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></li> </ul> </div>
五、打印指定路徑下a標籤的屬性(能夠經過遍歷拿到某個屬性的值,查找標籤的內容)
html = etree.HTML(wb_data) html_data = html.xpath('/html/body/div/ul/li/a/@href') for i in html_data: print(i) 打印: link1.html link2.html link3.html link4.html link5.html
六、咱們知道咱們使用xpath拿到得都是一個個的ElementTree對象,因此若是須要查找內容的話,還須要遍歷拿到數據的列表。
查到絕對路徑下a標籤屬性等於link2.html的內容。
html = etree.HTML(wb_data) html_data = html.xpath('/html/body/div/ul/li/a[@href="link2.html"]/text()') print(html_data) for i in html_data: print(i) 打印: ['second item'] second item
七、上面咱們找到所有都是絕對路徑(每個都是從根開始查找),下面咱們查找相對路徑,例如,查找全部li標籤下的a標籤內容。
html = etree.HTML(wb_data) html_data = html.xpath('//li/a/text()') print(html_data) for i in html_data: print(i) 打印: ['first item', 'second item', 'third item', 'fourth item', 'fifth item'] first item second item third item fourth item fifth item
八、上面咱們使用絕對路徑,查找了全部a標籤的屬性等於href屬性值,利用的是/---絕對路徑,下面咱們使用相對路徑,查找一下l相對路徑下li標籤下的a標籤下的href屬性的值,注意,a標籤後面須要雙//。
html = etree.HTML(wb_data) html_data = html.xpath('//li/a//@href') print(html_data) for i in html_data: print(i) 打印: ['link1.html', 'link2.html', 'link3.html', 'link4.html', 'link5.html'] link1.html link2.html link3.html link4.html link5.html
九、相對路徑下跟絕對路徑下查特定屬性的方法相似,也能夠說相同。
html = etree.HTML(wb_data) html_data = html.xpath('//li/a[@href="link2.html"]') print(html_data) for i in html_data: print(i.text) 打印: [<Element a at 0x216e468>] second item
十、查找最後一個li標籤裏的a標籤的href屬性
html = etree.HTML(wb_data) html_data = html.xpath('//li[last()]/a/text()') print(html_data) for i in html_data: print(i) 打印: ['fifth item'] fifth item
十一、查找倒數第二個li標籤裏的a標籤的href屬性
html = etree.HTML(wb_data) html_data = html.xpath('//li[last()-1]/a/text()') print(html_data) for i in html_data: print(i) 打印: ['fourth item'] fourth item
十二、若是在提取某個頁面的某個標籤的xpath路徑的話,能夠以下圖:
//*[@id="kw"]
解釋:使用相對路徑查找全部的標籤,屬性id等於kw的標籤。
#!/usr/bin/env python # -*- coding:utf-8 -*- from scrapy.selector import Selector, HtmlXPathSelector from scrapy.http import HtmlResponse html = """<!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF-8"> <title></title> </head> <body> <ul> <li class="item-"><a id='i1' href="link.html">first item</a></li> <li class="item-0"><a id='i2' href="llink.html">first item</a></li> <li class="item-1"><a href="llink2.html">second item<span>vv</span></a></li> </ul> <div><a href="llink2.html">second item</a></div> </body> </html> """ response = HtmlResponse(url='http://example.com', body=html,encoding='utf-8') # hxs = HtmlXPathSelector(response) # print(hxs) # hxs = Selector(response=response).xpath('//a') # print(hxs) # hxs = Selector(response=response).xpath('//a[2]') # print(hxs) # hxs = Selector(response=response).xpath('//a[@id]') # print(hxs) # hxs = Selector(response=response).xpath('//a[@id="i1"]') # print(hxs) # hxs = Selector(response=response).xpath('//a[@href="link.html"][@id="i1"]') # print(hxs) # hxs = Selector(response=response).xpath('//a[contains(@href, "link")]') # print(hxs) # hxs = Selector(response=response).xpath('//a[starts-with(@href, "link")]') # print(hxs) # hxs = Selector(response=response).xpath('//a[re:test(@id, "i\d+")]') # print(hxs) # hxs = Selector(response=response).xpath('//a[re:test(@id, "i\d+")]/text()').extract() # print(hxs) # hxs = Selector(response=response).xpath('//a[re:test(@id, "i\d+")]/@href').extract() # print(hxs) # hxs = Selector(response=response).xpath('/html/body/ul/li/a/@href').extract() # print(hxs) # hxs = Selector(response=response).xpath('//body/ul/li/a/@href').extract_first() # print(hxs) # ul_list = Selector(response=response).xpath('//body/ul/li') # for item in ul_list: # v = item.xpath('./a/span') # # 或 # # v = item.xpath('a/span') # # 或 # # v = item.xpath('*/a/span') # print(v)