如何通过xpath提取Div的对应文本? [英] How to extract the corresponding text of a Div via xpath?

查看:181
本文介绍了如何通过xpath提取Div的对应文本?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在使 xpath 从下面给定的 HTML 节点中提取数据时,我无法从 Div 中的相应元素中提取相应的文本.

<div class="Sub"><div class="Birth">Jack</div><span class="日期"><div><span class="Date">6 June 2018</span></div></span></div><div class="Sub"><div class="Birth">Hurley</div><span class="Date"><div><span class="Date">2011 年 6 月 21 日</span></div></span></div><div class="Sub"><div class="Birth">凯特</div><span class="日期"><div><span class="Date">2013 年 5 月 11 日</span></div></span></div><div class="Sub"><div class="Birth">John</div><span class="日期"><div><span class="Date">2001 年 3 月 5 日</span></div></span></div>

我想要的是根据

中的文本提取

中的Date文本..映射数据提取数据的问题['杰克'、'赫尔利'、'凯特'、'约翰']通过xpath('//*[@class="Birth"]/text()').extract() 和['2018 年 6 月 6 日'、'2011 年 6 月 21 日'、'2013 年 5 月 11 日'、'2001 年 3 月 5 日'] 通过xpath('//*[@class="Date"]/text()').extract()是它们不一定按相同的顺序排列,因此需要在 div 内进行相对映射,因为可以看出所有段的 div 类的名称都相同.为了确保它必须像,对于文本元素凯特 - 日期是 2013 年 5 月 11 日.

解决方案

我不确定兄弟姐妹,但迭代方法可以解决:

for i in range(0, len(list)):如果 list_search[i] == "Jack":更新 = div[i] 中的对应值休息

While making xpath to extract data out of the below given HTML nodes, I'm unable to extract the corresponding text from corresponding elements within a Div.

<div class="Main">
    <div class="Sub">
        <div class="Birth">Jack</div>
        <span class="Date">
            <div><span class="Date">6 June 2018</span></div></span></div>
    <div class="Sub">
        <div class="Birth">Hurley</div>
        <span class="Date"><div><span class="Date">21 June 2011</span></div></span></div>
    <div class="Sub">
        <div class="Birth">Kate</div>
        <span class="Date">
            <div><span class="Date">11 May 2013</span></div></span></div>
    <div class="Sub">
        <div class="Birth">John</div>
        <span class="Date">
            <div><span class="Date">5 March 2001</span></div></span></div>

What I want is to extract Date text in <div><span class="Date"> against the text in <div class="Birth">. The problem in mapping the data extracted data ['Jack','Hurley','Kate','John'] via xpath('//*[@class="Birth"]/text()').extract() and ['6 June 2018','21 June 2011','11 May 2013','5 March 2001'] via xpath('//*[@class="Date"]/text()').extract() is that they are not necessarily going to be in the same order, thus a relative mapping within the div is required as it can be seen that the name of the div class are same for all segments. For being sure it must be like, For Text element Kate - Date is 11 May 2013.

解决方案

I'm not sure about siblings but iteration method can work it out as:

for i in range(0, len(list)):
            if list_search[i] == "Jack":
                    Updated = corresponding-value-in-div[i]
                    break

这篇关于如何通过xpath提取Div的对应文本?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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