crawl——xpath使用

一、xpath的使用

1 css  xpath  都是通用的选择器
2 XPath 使用路径表达式在 XML 文档中选取节点
3 lxml模块为例,讲xpath选择(selenium,scrapy---》css/xpath)
4 主要用法:

# / :从当前节点开始选择,子节点
# // :从当前节点开始选择,子子节点
# @选取属性a[@href="image1.html"],选择a标签,href属性为image1.html
# .. 表示父节点
# /a[3] 选当前节点下第一个a标签
# /text() 取这个标签的文本
# /@href  获取href属性
# /a[contains(@class,"li")] 类名中有li的a标签
# /a[@class='li'] 类名等于li的a标签

代码演示

# pip3 install lxml

doc='''
<html>
 <head>
  <base href='http://example.com/' />
  <title>Example website</title>
 </head>
 <body>
  <div id='images' name='lqz'>
   <a href='image1.html' class='li' name='xx' id='id_a' >Name: My image 1 <br /><img src='image1_thumb.jpg' /></a>
   <a href='image2.html'>Name: My image 2 <br /><img src='image2_thumb.jpg' /></a>
   <a href='image3.html'>Name: My image 3 <br /><img src='image3_thumb.jpg' /></a>
   <a href='image4.html'>Name: My image 4 <br /><img src='image4_thumb.jpg' /></a>
   <a href='image5.html' class='li li-item' name='items'>Name: My image 5 <br /><img src='image5_thumb.jpg' /></a>
   <a href='image6.html' name='items'><span><h5>test</h5></span>Name: My image 6 <br /><img src='image6_thumb.jpg' /></a>
  </div>
 </body>
</html>
'''

from lxml import etree

html=etree.HTML(doc)
# html=etree.parse('search.html',etree.HTMLParser())

# / :从当前节点开始选择,子节点
# // :从当前节点开始选择,子子节点
# res=html.xpath('/html/head')
# res=html.xpath('//head')
# res=html.xpath('/html/body/div/a')
# res=html.xpath('//a')

# * 所有节点
# res=html.xpath('//*') # 找子子孙孙所有的标签
# res=html.xpath('/*')    # 只找根下的所有标签,只有html
# res=html.xpath('/html/body/*')    # 找html下body下的所有标签(只找子,不找孙)
# res=html.xpath('/html/body//*')    # 找html下body下的所有标签(子子孙孙)
#
#
# print(res)



# 1 所有节点
# a=html.xpath('//*')



# 2 指定节点(结果为列表)
# a=html.xpath('//head')

# 3 子节点,子孙节点
# a=html.xpath('//div/a')
# a=html.xpath('//body/a') #无数据
# a=html.xpath('//body//a')



# @选取属性a[@href="image1.html"],选择a标签,href属性为image1.html

# .. 表示父节点
# /a[1] 选当前节点下第一个a标签
# 4 父节点
# a=html.xpath('//body//a[@href="image1.html"]/..')
# a=html.xpath("//div[@id='images']")
# a=html.xpath('//body//a[1]/..')
# a=html.xpath('//body//a[3]')
# 也可以这样(了解)
# a=html.xpath('//body//a[1]/parent::*')




# 5 属性匹配
# a=html.xpath('//a[@href="image1.html"]')



# 6 文本获取   取这个标签的文本/text()
# a=html.xpath('//a[@href="image1.html"]/text()')
# a=html.xpath('//a/text()')
# a=html.xpath('//h5/text()')


## 获取属性 /@href  获取href属性
# # 注意从1 开始取(不是从0)
# a=html.xpath('//a[1]/@href')
# a=html.xpath('//a/@href')
# a=html.xpath('//div[@id="images"]/@id')
# a=html.xpath('//div[@id="images"]/@name')




# 8 属性多值匹配
#  a 标签有多个class类,直接匹配就不可以了,需要用contains
# a=html.xpath('//body//a[@class="li"]')
# a=html.xpath('//body//a[contains(@class,"li")]')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')



## 以下都是了解

# 9 多属性匹配
# a=html.xpath('//a[contains(@class,"li") or @name="items"]')
# a=html.xpath('//body//a[contains(@class,"li") and @name="items"]/text()')
# a=html.xpath('//body//a[contains(@class,"li")]/text()')
# 10 按序选择
# a=html.xpath('//a[2]/text()')
# a=html.xpath('//a[2]/@href')
# 取最后一个
# a=html.xpath('//a[last()]/@href')
# 位置小于3的  position()
# a=html.xpath('//a[position()<3]/@href')
# 倒数第二个
# a=html.xpath('//a[last()-2]/@href')
# 11 节点轴选择
# ancestor:祖先节点
# 使用了* 获取所有祖先节点
# a=html.xpath('//a/ancestor::*')
# # 获取祖先节点中的div
# a=html.xpath('//a/ancestor::div')
# a=html.xpath('//a/ancestor::html')
# attribute:属性值
# a=html.xpath('//a[1]/attribute::*')
# child:直接子节点
# a=html.xpath('//a[1]/child::*')
# descendant:所有子孙节点
# a=html.xpath('//a[6]/descendant::*')
# following:当前节点之后所有节点
# a=html.xpath('//a[1]/following::*')
# a=html.xpath('//a[1]/following::*[1]/@href')
# following-sibling:当前节点之后同级节点
# a=html.xpath('//a[1]/following-sibling::*')
# a=html.xpath('//a[1]/following-sibling::a')
# a=html.xpath('//a[1]/following-sibling::*[2]')
a=html.xpath('//a[1]/following-sibling::*[2]/@href')


print(a)

二、xpath举例

import requests
from lxml import etree
res=requests.get('https://www.cnblogs.com/xiaoyuanqujing/p/11805679.html')


html=etree.HTML(res.text)
res=html.xpath('//*[@id="cnblogs_post_body"]/p[8]/a/@href')
print(res)

 

posted @ 2021-01-23 19:45  1024bits  阅读(74)  评论(0编辑  收藏  举报