Python爬蟲之xpath使用技巧
使用方法都寫在程序裏面的註釋中,請盡情享用,如果您覺得不錯可以點個贊哦🙂
代碼如下:
"""<<常用表達式規則>>
表達式: 含義:
nodename 選擇此節點的所有子節點
/ 從當前節點選取直接子節點
// 從當前節點選取子孫節點
. 選取當前節點
.. 選取當前節點的父節點
@ 選取屬性
* 選取所有信息
<<多屬性匹配運算符介紹>>
運算符: 描述: 示例:
and 與 age=19 or age=20
or 或 age>19 and age<21
mod 計算除法的餘數 5 mod 7
| 計算兩個節點集 //book | //cd
+ 加法 6 + 4
- 減法 6 - 4
* 乘法 6 * 4
div 除法 8 div 4
= 等於 age=19
!= 不等於 age!=19
< 小於 age<19
<= 小於等於 age<=19
> 大於 age>19
>= 大於等於 age>=19
"""
# -*- coding:utf-8 -*-
from lxml import etree
__author__ = 'Evan'
html_doc = """
<html>
<head><title>The Dormouse's story</title></head>
<body>
<p class="title"><b>The Dormouse's story</b></p>
<ul class="list" id="list-1">
<li class="element"><a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>Foo</li>
<li class="element">Bar</li>
<li class="element">Jay</li>
</ul>
<ul class="list two" id="list-2">
<li class="element"><a href="http://example.com/tillie" class="parent" id="link3">Tillie</a>evan</li>
<li class="element">jane</li>
<li class="element">summer</li>
</ul>
<p class="story">Once upon a time there were three little sisters; and their names were
<a href="http://example.com/elsie" class="sister" id="link1">Elsie</a>,
<a href="http://example.com/lacie" class="child" id="link2">Lacie</a> and
<a href="http://example.com/tillie" class="parent" id="link3">Tillie</a>;
and they lived at the bottom of a well.</p>
<p class="story">End...</p>
</body>
</html>
"""
def parse():
"""
使用xpath解析HTML
:return:
"""
# 初始化對象
html = etree.HTML(html_doc) # html文本初始化
# html = etree.parse('./example.html', etree.HTMLParser()) # html文件初始化
# 補全HTML代碼
print(etree.tostring(html).decode('utf-8'))
# 文本獲取
print(html.xpath('//a[@class="parent"]/text()')) # 獲取所有a節點內class等於'parent'的文本,返回一個列表
# 屬性獲取
print(html.xpath('//ul/@class')) # 獲取所有ul節點內的class值,返回一個列表
print(html.xpath('//ul/attribute::*')) # 獲取所有ul節點內的所有屬性值,返回一個列表
# 屬性匹配
print(html.xpath('//ul[@class="list"]')) # 獲取所有class等於'list'的ul節點,返回一個列表
# 屬性多值匹配
print(html.xpath('//ul[contains(@class, "two")]')) # 獲取所有class包含'two'的ul節點,返回一個列表
# 多屬性匹配
print(html.xpath('//ul[contains(@class, "two") and @id="list-2"]')) # 滿足上面的情況再加上id等於'list-2',返回一個列表
# 獲取所有節點
print(html.xpath('//*')) # 獲取所有節點,返回一個列表
print(html.xpath('//li')) # 獲取所有的li節點,返回一個列表
# 獲取父·祖先節點
print(html.xpath('//li/parent::*')) # 獲取所有li節點的直接父節點,返回一個列表
print(html.xpath('//li/..')) # 用法同上
print(html.xpath('//li/ancestor::*')) # 獲取所有li節點的祖先節點,返回一個列表
print(html.xpath('//li/ancestor::ul')) # 獲取所有li節點的ul祖先節點,返回一個列表
# 獲取子·子孫節點
print(html.xpath('//ul/child::*')) # 獲取所有ul節點內的直接子節點,返回一個列表
print(html.xpath('//ul/child::li')) # 獲取所有ul節點內的li直接子節點,返回一個列表
print(html.xpath('//ul/li')) # 用法同上
print(html.xpath('//ul/descendant::*')) # 獲取所有ul節點內的子孫節點,返回一個列表
print(html.xpath('//ul/descendant::a')) # 獲取所有ul節點內的a子孫節點,返回一個列表
print(html.xpath('//ul//a')) # 用法同上
# 獲取兄弟·後續節點
print(html.xpath('//li[1]/following-sibling::*')) # 獲取所有li[1]節點之後的兄弟節點,返回一個列表
print(html.xpath('//li[1]/following::*')) # 獲取所有li[1]節點的後續節點,返回一個列表
print(html.xpath('//li[1]/following::*[2]')) # 獲取所有li[1]節點後的第二個節點,返回一個列表
# 按序選擇(正序位置是從1開始,last()-2 代表倒數第三個位置,因爲last()是最後一個)
print(html.xpath('//ul/li[1]')) # 獲取所有ul節點內的第一個li節點,返回一個列表
print(html.xpath('//ul/li[last()]')) # 獲取所有ul節點內的最後一個li節點,返回一個列表
print(html.xpath('//ul/li[last()-2]')) # 獲取所有ul節點內的倒數第三個li節點,返回一個列表
print(html.xpath('//ul/li[position()<3]')) # 獲取所有ul節點內位置小於3的li節點,返回一個列表
if __name__ == '__main__':
parse()