XPATH爬取的一点记录

贴一小段代码,记录xpath爬取比较有用的前后兄弟节点

 # -*- coding: utf-8 -*-
from lxml import etree
import requests

#网页代码见下面截图
url = 'aaaa'
res = requests.get(url)

ress = etree.HTML(res.content)
#这里一开始用的res.text,报错:Unicode strings with encoding declaration are not supported. Please use bytes input or XML fragments without declaration.
找了一番资料,改成res.content发现就可以了,目前还不清楚为什么。

outt1 = ress.xpath('//span[contains(string(),"outstanding")]')[0]
#定位文本中包含这个单词的标签,是列表形式,要索引出来

outt 在这里插入代码片= outt1 .xpath('./preceding-sibling::span[3]//text()')
#当前这个节点的往上数第三个span节点,因为有嵌套,用的//
datee = outt1 .xpath('./following-sibling::span[1]/text()')
#当前这个节点往下数第一个节点
print(outt)
print(datee)

代码截图

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值