Brief introduction
XPath is the
XML
Path Language (XML Path Language), which is an XML document is used to determine the position of a part of the language.
XPath-based XML tree structure, providing the ability to find a node in the data tree. XPath mind initially proposed that it as a general-purpose, between
XPointer
and
XSL
grammar model between. But XPath soon be employed to developers as a small
query language
.
grammar
Select XPath uses path expressions to select nodes nodes in an XML document. Along the path through the node, or to select a step.
[1]
Listed below are the most useful path expressions:
expression
|
description
|
---|---|
nodename
|
Select all the child nodes of this node.
|
/
|
Choose from the root node.
|
//
|
Select the document matches the selected node from the current node, regardless of their location.
|
.
|
Select the current node.
|
..
|
Select the parent of the current node.
|
@
|
Select Properties.
|
Operators
Operators
|
description
|
Examples
|
return value
|
---|---|---|---|
|
|
Calculating two sets nodes
|
//book | //cd
|
Returns the node set has all the elements of the book and cd
|
+
|
addition
|
6 + 4
|
10
|
-
|
Subtraction
|
6 - 4
|
2
|
*
|
multiplication
|
6 * 4
|
24
|
div
|
division
|
8 div 4
|
2
|
=
|
equal
|
price=9.80
|
If the price is 9.80, it returns true.
If the price is 9.90, false is returned.
|
!=
|
not equal to
|
price!=9.80
|
If the price is 9.90, it returns true.
If the price is 9.80, false is returned.
|
<
|
Less than
|
price<9.80
|
If the price is 9.00, it returns true.
If the price is 9.90, false is returned.
|
<=
|
less than or equal to
|
price<=9.80
|
If the price is 9.00, it returns true.
If the price is 9.90, false is returned.
|
>
|
more than the
|
price>9.80
|
If the price is 9.90, it returns true.
If the price is 9.80, false is returned.
|
>=
|
greater than or equal to
|
price>=9.80
|
If the price is 9.90, it returns true.
If the price is 9.70, false is returned.
|
or
|
or
|
price=9.80 or price=9.70
|
If the price is 9.80,
Or the price is 9.70, it returns true.
|
and
|
versus
|
price>9.00 and price<9.90
|
If the price is greater than 9.00,
And the price is less than 9.90, it returns true.
|
mod
|
Calculated remainder of the division
|
5 mod 2
|
1
|
Common expressions
- 获取所有节点 - 获取所有li标签 - //* //li - 获取子节点 - 我们通过/或//即可查找元素的子节点和子孙节点 - li节点的所有直接a子节点 - //li/a - 获取ul的所有子孙a节点 - //ul//a - 获取父节点属性 - 知道子节点查询父节点 - //div[@class="filter-wrap"]/../@class' - //div[@class="filter-wrap"]/parent::*/@class' - 属性定位 - 找到当前源码中所有class属性为song的div标签 - //div[@class="song"] - 层级&索引定位 - 找到class属性值为tang的div的直系子标签ul下的第二个子标签li下的直系子标签a - //div[@class="tang"]/ul/li[2]/a - 多属性匹配 - 从当前源码中找到href属性为空且class属性为song的所有a标签 - //a[@href="" and @class="song"] - 模糊匹配 - 找到所有class属性值中包含nb的所有div标签 - //div[contains(@class,"so")] - 找到所有class属性值以zb开头的所有div标签 - //div[starts-with(@class,"ta")] - 获取文本 - / 表示获取某个标签下的文本内容 - // 表示获取某个标签下的文本内容和所有子标签下的文本内容 - //div[@class="song"]/p[1]/text() - //div[@class="tang"]//text() - 获取属性 - //div[@class="tang"]//li[2]/a/@href
案例演示
环境安装
pip install lxml
解析原理
- 解析原理: - 获取页面源码数据 - 实例化一个etree对象,并将页面源码数据加载到该对象中 - 调用该对象的xpath方法进行指定标签的定位 - 注意:xpath函数必须结合着xpath表达式进行标签的定位和内容的捕获
58同城房源信息抓取
import requests from lxml import etree url = "https://bj.58.com/shahe/ershoufang/?PGTID=0d30000c-0047-e4b2-f57c-08960a90aab4&ClickID=1" headres = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.142 Safari/537.36' } response = requests.get(url).text # 实列化一个etree对象,加载页面源码数据 tree = etree.HTML(response) # etree对象调用xpath函数,结合xpath表达式进行标签定位和内容捕获 li_list = tree.xpath('//ul[@class="house-list-wrap"]/li') # 返回值由很多个li标签组成的列表 for i in li_list: tetle = i.xpath("./div[2]/h2/a/text()")[0] # 局部调用表达式时必须加点 procr = "".join(i.xpath("./div[3]/p//text()")) # 将价格拼接 print(tetle,procr)