大家好,又见面了,我是全栈君
xpath是一种在XML文档中定位元素的语言,常用于xml、html文件解析,比css选择器使用方便 XML文件最小构成单元: - element(元素节点) - attribute(属性节点) - text(文本) - namespace(命名空间) - processing-instruction(命令处理) - comment(注释) - root(根节点)
xpath通过”路径表达式”来选择节点,在表现形式上与传统的文件系统类似
绝对路径(absolute path)必须用”/”起首,后面紧跟根节点,比如/step/step/…
相对路径(relative path)则是除了绝对路径以外的其他写法,比如 step/step,也就是不使用”/”起首
斜杠(/)作为路径内部的分割符
”/”:表示选择根节点
”//”:表示选择任意位置的某个节点
”@”: 表示选择某个属性
nodename(节点名称):表示选择该节点的所有子节点
使用功能函数能够更好的进行模糊搜索
函数 | 用法 | 解释 |
---|---|---|
starts-with | xpath(‘//div[starts-with(@id,”ma”)]‘) | 选取id值以ma开头的div节点 |
contains | xpath(‘//div[contains(@id,”ma”)]‘) | 选取id值包含ma的div节点 |
and | xpath(‘//div[contains(@id,”ma”) and contains(@id,”in”)]‘) | 选取id值包含ma和in的div节点 |
text() | xpath(‘//div[contains(text(),”ma”)]‘) | 选取节点文本包含ma的div节点 |
<?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>
#bookstore :选取 bookstore 元素的所有子节点。
#/bookstore :选取根节点bookstore,这是绝对路径写法。
#bookstore/book :选取所有属于 bookstore 的子元素的 book元素,这是相对路径写法。
#//book :选择所有 book 子元素,而不管它们在文档中的位置。
#bookstore//book :选择所有属于 bookstore 元素的后代的 book 元素,而不管它们位于 bookstore 之下的什么位置。
#//@lang :选取所有名为 lang 的属性。
#/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/book[price] :表示选择bookstore的book子元素,且被选中的book元素必须带有price子元素。
#/bookstore/book[price>35.00] :表示选择bookstore的book子元素,且被选中的book元素的price子元素值必须大于35。
#/bookstore/book[price>35.00]/title :表示在例14结果集中,选择title子元素。
#/bookstore/book/price[.>35.00] :表示选择值大于35的"/bookstore/book"的price子元素。
#//book/title | //book/price :表示同时选择book元素的title子元素和price子元素。
# 我们爬取网页的目的,无非是先定位到DOM树的节点,然后取其文本或属性值
myPage = '''<html>
<title>TITLE</title>
<body>
<h1>我的博客</h1>
<div>我的文章</div>
<div id="photos">
<img src="pic1.jpeg"/><span id="pic1">PIC1 is beautiful!</span>
<img src="pic2.jpeg"/><span id="pic2">PIC2 is beautiful!</span>
<p><a href="http://www.example.com/more_pic.html">更多美图</a></p>
<a href="http://www.baidu.com">去往百度</a>
<a href="http://www.163.com">去往网易</a>
<a href="http://www.sohu.com">去往搜狐</a>
</div>
<p class="myclassname">Hello,\nworld!<br/>-- by Adam</p>
<div class="foot">放在尾部的其他一些说明</div>
</body>
</html>'''
html = etree.fromstring(myPage)
# 一、定位
divs1 = html.xpath('//div')
divs2 = html.xpath('//div[@id]')
divs3 = html.xpath('//div[@class="foot"]')
divs4 = html.xpath('//div[@*]')
divs5 = html.xpath('//div[1]')
divs6 = html.xpath('//div[last()-1]')
divs7 = html.xpath('//div[position()<3]')
divs8 = html.xpath('//div|//h1')
divs9 = html.xpath('//div[not(@*)]')
# 二、取文本 text() 区别 html.xpath('string()')
text1 = html.xpath('//div/text()')
text2 = html.xpath('//div[@id]/text()')
text3 = html.xpath('//div[@class="foot"]/text()')
text4 = html.xpath('//div[@*]/text()')
text5 = html.xpath('//div[1]/text()')
text6 = html.xpath('//div[last()-1]/text()')
text7 = html.xpath('//div[position()<3]/text()')
text8 = html.xpath('//div/text()|//h1/text()')
# 三、取属性 @
value1 = html.xpath('//a/@href')
value2 = html.xpath('//img/@src')
value3 = html.xpath('//div[2]/span/@id')
# 四、定位(进阶)
# 1.文档(DOM)元素(Element)的find,findall方法
divs = html.xpath('//div[position()<3]')
for div in divs:
ass = div.findall('a') # 这里只能找到:div->a, 找不到:div->p->a
for a in ass:
if a is not None:
#print(dir(a))
print(a.text, a.attrib.get('href')) #文档(DOM)元素(Element)的属性:text, attrib
# 2.与1等价
a_href = html.xpath('//div[position()<3]/a/@href')
print(a_href)
# 3.注意与1、2的区别
a_href = html.xpath('//div[position()<3]//a/@href')
print(a_href)
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/120200.html原文链接:https://javaforall.cn