xpath的使用(新手必看)

步骤:
1.从lxml中导入etree(两种方式)
第一种:

from lxml import etree

注意:第一种方式,可能etree下方会出现红线,但是不影响使用

第二种:

from lxml import html
etree = html.etree
str = 	'<html>' \
            '<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>' \
       '</html>'

2.etree.HTML() 将字符串转换成html元素对象(必须进行),可以自动补全缺失的标签

html = etree.HTML(str)
print(html)  # <Element html at 0x11524df0108>

3. 使用xpath进行数据提取
xpath是根据路径表达式来查找元素的,返回值是一个列表
如果没有找到,返回的是一个空列表
比如:F:\Python夏令营

xpath路径分为两种:
第一种:/ 如果作为开头,代表的是根路径,如果是在路径里面写,代表一层一层的查找
找到bookstore元素

bookstore = html.xpath('/html/bookstore')
print(bookstore)  # []  -> 没有找到
boostore = html.xpath('/html/body/booktore')
print(bookstore)

第二种:// 代表任意路径
找到bookstore元素

bookstore = html.xpath('//body/bookstore')  
print(bookstore)  # [<Element bookstore at 0x198c6960348>]

例如:获取book元素

book = html.xpath('//book')
print(book)  # [<Element book at 0x1f397370348>, <Element book at 0x1f397370388>]

text() 获取标签之间的内容
例如:获取title标签的内容
步骤:

  • 1.先获取到title标签
    2.再获取内容即可
title = html.xpath('//book/title')
print(title)  # [<Element title at 0x1e3b17b0448>, <Element title at 0x1e3b17b0488>]
title1 = html.xpath('//book/title/text()')
print(title1)  # ['Harry Potter', 'Learning XML']
str = 	'<html>' \
            '<bookstore>' \
                '<book>' \
                    '<title lang="ang">Harry Potter</title>' \
                    '<price>29.99</price>' \
                '</book>' \
                '<book>' \
                    '<title lang="eng">Learning XML</title>' \
                    '<price>39.95</price>' \
                '</book>' \
            '</bookstore>' \
       '</html>'
html = etree.HTML(str)

谓语:[]
如果条件是属性:需要使用@关键字
例如:获取lang属性为eng的title标签之间的内容

title = html.xpath('//book/title[@lang="eng"]/text()')
print(title)  # ['Learning XML']
str = 	'<html>' \
            '<bookstore>' \
                '<book>' \
                    '<title lang="ang">Harry Potter</title>' \
                    '<price>29.99</price>' \
                '</book>' \
                '<book>' \
                    '<title lang="eng" href="http://www.baidu.com">Learning XML</title>' \
                    '<price>39.95</price>' \
                '</book>' \
            '</bookstore>' \
       '</html>'
html = etree.HTML(str)

获取属性的值:/@+属性名
例如:获取所有title标签href属性的值
步骤:

  • 1.先获取title标签
    2.再获取href属性值
title = html.xpath('//book/title/@href')
print(title)  #['http://www.baidu.com']
  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值