眾多語言都能進行爬蟲,但基於python的爬蟲顯得更加簡潔,方便。爬蟲也成了python語言中不可或缺的一部分。爬蟲的解析方式也是多樣化。上一篇告訴大家的是爬蟲的解析方式四:PyQuery,今天帶給大家的是另一種方式,XPath。
python爬蟲之xpath的基本使用
一、簡介
XPath 是一門在XML 文件中尋找資訊的語言。 XPath 可用於在 XML 文件中對元素和屬性進行遍歷。 XPath 是 W3C XSLT 標準的主要元素,而 XQuery 和 XPointer 都建構在 XPath 表達之上。
二、安裝
pip3 install lxml
#三、使用
11 、導入
from lxml import etree
2、基本使用
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)則是一個python對象,etree.tostring(html)則是一個python對象,etree.tostring(html)則是一個python對象,etree.tostring(html)則是一個python對象,etree.tostring(html)不全裡html的基本寫法,補全了缺手臂少腿的標籤。
<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>
3、取得某個標籤的內容(基本上使用),注意,取得a標籤的所有內容,a後面就不用再加正斜杠,否則報錯。
寫法一
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
4、開啟讀取html檔案
4、開啟讀取html檔案
4、開啟讀取html檔案
4、開啟讀取html檔案
#使用parse打开html的文件 html = etree.parse('test.html') html_data = html.xpath('//*')<br>#打印是一个列表,需要遍历 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>
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
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)
7、上面我們找到全部都是絕對路徑(每一個都是從根開始查找),下面我們查找相對路徑,例如,查找所有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
### 8、上面我們使用絕對路徑,找出了所有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
10、查找最后一个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
11、查找倒数第二个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
12、如果在提取某个页面的某个标签的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> <meta charset="UTF-8"> <title></title> </head> <body> <ul> <li><a id='i1' href="link.html">first item</a></li> <li><a id='i2' href="llink.html">first item</a></li> <li><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)
以上是爬蟲的解析方式五:XPath的詳細內容。更多資訊請關注PHP中文網其他相關文章!