python爬蟲之Xpath
了解XML 和xpath
Xpath:http://www.w3school.com.cn/xpath/index.asp
安裝庫
Pip install lxml
什麽是xpath
Xml是用來存儲和傳輸數據使用的
和html的不同有兩點:
- html用來顯示數據,xml是用來傳輸數據
- Html標簽是固定的,xml標簽是自定義的
Xpath用來在xml中查找指定的元素,它是一種路徑表達式。
常用的路徑表達式:
// :不考慮位置的查找
./ : 從當前節點開始往下查找
@ :選取屬性
實例
‘’’
<?xml version="1.0" encoding="ISO-8859-1"?>
<bookstore>
<book>
<title lang="eng">Harry Potter</title>
<price>29.99</price>
</book>
<book>
<title lang="eng">Learning XML</title>
<price>39.95</price>
</book>
</bookstore>
‘’’
Ps:
/bookstore/book 選取根節點
//book 選取所有的book元素,而不管它們在文檔中的位置。
bookstore//book 選擇屬於 bookstore 元素的後代的所有 book 元素,而不管它們位於 bookstore 之下的什麽位置 (還可以找孫子)
/bookstore/book[1] bookstore 下的的第一個 book 元素
/bookstore/book[last()] bookstore的最後一個 book 元素
/bookstore/book[last()-1] 選取屬於 bookstore 子元素的倒數第二個 book 元素
/bookstore/book[position()<3] 選取最前面的兩個屬於 bookstore 元素的子元素的 book 元素。
//title[@lang] 所有帶有 lang 的屬性的 title 元素。
//title[@lang=‘eng‘] 所有的lang屬性為eng的title節點
* 匹配任何元素節點。
/bookstore/* 選取 bookstore 元素的所有子元素。
//* 選取文檔中的所有元素。
/title[@*] 選取所有帶有屬性的 title 元素。
安裝xpath插件
將xpath插件拖動到谷歌瀏覽器擴展程序中,安裝成功
啟動和關閉插件:ctrl+shift+x
以百度首頁為例子:
屬性定位
//input[@id=”kw”] 這是搜索框的路徑表達式
//input[@class=”bg s_btn”] 這是百度一下的路徑表達式
層級定位
//div[@id=”head”]/div/div[2]/a[@class=”toindex”]
曾經加索引 這是百度一下的路徑表達式
Ps:索引從1開始
//div[id=:”head”]//a[@class=”toindex”]
Ps:雙斜杠代表下面所有的a節點,不管位置
邏輯運算
//input[@class=”s_ipt”and @name=”wd”]
模糊匹配
Contains
//input[contains(@class,”s_i”)]
所有的input 有class屬性 並且屬性中帶有s_i的節點
starts-with
//input[start-with(@class,”s”)]
所有的input 有class屬性 並且屬性以s開頭的節點
Ps:沒有endwith
取文本
//div[@id=”ul”/a[5]/text()] #貼吧 獲取節點內容
//div[@id=”ul”/text()] #貼吧 獲取節點內不帶標簽的所有內容
取屬性
//div[@id=”ul”/a[5]/@href 獲取屬性herf
代碼中操作xpath
導入庫
from lxml import etree
兩種方式使用:都是將html文檔變成一個對象,然後調用對象的方法去查找指定的節點
(1) 本地文件
tree = etree.parse(文件名)
(2) 網絡文件
tree=etree.HTML(網頁字符串)
例子:
from lxml import etree
#生成對象
tree = etree.paese(“xpath.html”)
ret = tree.xpath(‘//div[@class=”tang”]/ul/li[1]/text()’) #打印出內容
Print(ret) #這裏是一個列表
‘’’
另一種寫法
ret = tree.xpath(‘//div[@class=”tang”]/ul/li[1]’)
print(ret[0].text)
‘’’
ret = tree.xpath(路徑表達式)
ret是一個列表
from lxml import etree
tree = etree.paese(“xpath.html”)
ret = tree.xpath(‘//div[@class=”tang”]/ul/li[last()]/a/@href’)#取href屬性
print(ret)
from lxml import etree
tree = etree.paese(“xpath.html”)
ret = tree.xpath(‘//div[@class=”tang”]/ul/li[@class=”love”
and @name=”yang”]’)
print(ret[0].text)
#取出\n ,\t 換成空字符串直接獲得純文本
直接將所有的內容拼接起來
ret = tree.xpath(‘//div[@class=”song”]’)
string = ret[0].xpath(‘string(.)’)
print(string.replace(‘\n’,’’).replace(‘\t’,’’))#直接將所有的內容拼接起來
這裏是
‘’’
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)
etree.parse()
# print(html)
print(type(html)) # <class ‘lxml.etree._Element‘>
# 與之前這個類型類似 bs4.element.Tag
# print(html.xpath(‘li‘)) # []
# print(html.xpath(‘/li‘)) # []
# print(html.xpath(‘//li‘)) # [多個element]
print(html.xpath(‘//a‘)) # 取 元素當中的內容
# print(html.xpath(‘//a/text()‘)) # 取 元素當中的內容
print(html.xpath(‘//a/@href‘)) # 取 元素當中的屬性
# print(html.xpath(‘//li[@class="item-0"]//text()‘))
# print(html.xpath(‘//li[@class="item-0"]//@href‘))
print(html.xpath(‘//li[@class="item-0"]/a/text()‘))
‘’’
python爬蟲之Xpath