pingfanren2 2021-03-23 22:38 采纳率: 80%
浏览 26
已采纳

python xpath对孙节点内容进行匹配问题

首先查看节点结构如下,需要获取节点summaryrecordstable下的所有后代节点中,具有数据值id=RECORD_[0-9]的节点

代码如下

import requests
from lxml import etree

url_base='https://apps.webofknowledge.com'
url_test='https://apps.webofknowledge.com/full_record.do?product=WOS&search_mode=GeneralSearch&qid=2&SID=6C4xx6Mer35kGYw4PU7&page=1&doc=50'
url_head={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.90 Safari/537.36 Edg/89.0.774.57'
}
session=requests.session();
res=session.get(url_test,headers=url_head)
res_html = etree.HTML(res.text)
url_cited_post=res_html.xpath('//a[@title="View all of the articles that cite this one"]/@href')#如果把@herf替换为text(),获取不到信息,因为a标签只有属性没有文本
print(url_cited_post[0])
url_allcited=url_base+url_cited_post[0]
res=session.get(url_allcited,headers=url_head)
res_html = etree.HTML(res.text)
# url_cited_post=res_html.xpath('//div[contains(@id,"RECORD_")]')
url_cited_post=res_html.xpath('//div[contains(@id,"summaryRecordsTable")]//div[contains(@id,"RECORD_")]')
print('end')

倒数第三行,井号注释哪一行可以正常输出,但是加上前缀后,如倒数第二行,就不能正常输出了,不知道是怎么回事

 

  • 写回答

1条回答 默认 最新

  • coagenth 2021-03-23 23:05
    关注

    用following定位,把前面设为顶节点,following后利用属性值再定位子孙节点。

    url_cited_post = res_html.xpath(

        './/div[contains(@id,"summaryRecordsTable")]//following::div[contains(@id,"RECORD_")]')

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘
  • ¥15 perl MISA分析p3_in脚本出错
  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?