如何使用XPath从子节点获取文本(如果它是Scrapy中其他节点的父节点) [英] How to get the text from child nodes if it is parents to other node in Scrapy using XPath

查看:56
本文介绍了如何使用XPath从子节点获取文本(如果它是Scrapy中其他节点的父节点)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我面临一个问题,我必须从子节点获取结果,该子节点可能或可能不是使用scrapy的Xpath的某个其他节点的父节点.考虑这样的情况

I am facing a problem where I have to get the result from the child node which may or may not be parents to some other node using Xpath in scrapy. consider the case like

<h1 class="main">
 <span class="child">data</span>
</h1>

<h1 class="main">
<span class="child">
 <span class="child2">data</span>
</span>
</h1>

我的解决方案是 response.xpath(".//h1 [@ class ='main']/span/text()").extract()

推荐答案

使用//text ,它将返回 span 中列表中的所有文本元素,父母和孩子:

use //text, and it will return all text elements in a list from within your span, both parent and child:

response.xpath(".//h1[@class='main']/span//text()").extract()

这篇关于如何使用XPath从子节点获取文本(如果它是Scrapy中其他节点的父节点)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆