【问题标题】:How to extract text content between two node如何在两个节点之间提取文本内容
【发布时间】:2019-11-20 17:23:35
【问题描述】:

我想提取红色和绿色矩形中包含的文本,如下面的屏幕截图所示, 注意:文本不包含在开始和结束标记中

http://temperate.theferns.info/plant/Acacia+omalophylla

例如,对于绿色矩形的文本,我测试了这个 xpath 查询和以下代码(python/selenium):

greenrec_xpath = "//*[preceding::h3[contains(text(), 'General Information')] and following::h3[contains(text(), 'Known Hazards')]]"
driver.find_elements_by_xpath(greenrec_xpath)

但没有得到预期的结果

任何想法!

【问题讨论】:

  • 你可以通过 By.Xpath("//div[@class='family']/following-sibling::br[1]") 然后 .Text() .第二批比较棘手,因为没有标签。
  • Bertrand,你试过我的解决方案了吗?解决问题了吗?
  • 谢谢! Dimitre Novatchev,明天我将发布解决方案!

标签: javascript python selenium xpath webdriverwait


【解决方案1】:

当文本周围没有直接的括号时,它被称为文本节点,并且查找起来有点棘手,因为它不能像您尝试的那样直接访问。我通常要做的是找到直接父母的位置,并从中获取文本。如果该父节点下有多个文本节点,这会变得有点棘手,并且通常需要在获得整个文本后进行一些解析/拆分。

或者,如果您处于可以保证文本节点包含某些特定文本的情况下,您可以将text(). 交换并以这种方式创建xpath。例如: //*[contains(.,'Acacia omalophylla')]

【讨论】:

    【解决方案2】:
    greenrec_xpath = 
     "//*[preceding::h3[contains(text(), 'General Information')] 
        and following::h3[contains(text(), 'Known Hazards')]]"
    

    您已经非常接近找到选择所需文本节点的 XPath 表达式了:

    使用

    //*[preceding::h3[1][contains(., 'General Information')] 
      and following::h3[1][contains(., 'Known Hazards')]
       ]/text()[normalize-space()]
    

    请注意,此表达式选择了许多文本节点(在本例中为 5)。

    如果要获取单个字符串,则需要获取每个选定文本节点的字符串值,并将它们连接在一起形成一个字符串。如果您只能使用 XPath 1.0,则需要在调用编程(非 XPath)代码中执行此字符串连接。

    如果您可以使用 XPath 2.0(或更高版本),请使用

    string-join(
                //*[preceding::h3[1][contains(., 'General Information')] 
                  and following::h3[1][contains(., 'Known Hazards')]
                   ]/text()[normalize-space()]/string(.)
                ,
                 ''
               )
    

    【讨论】:

      【解决方案3】:

      要提取文本相思属的分类...作为元素是一个文本节点,你需要为visibility_of_element_located()诱导WebDriverWait,你可以使用以下Locator Strategy

      • 代码块:

        driver.get("http://temperate.theferns.info/plant/Acacia+omalophylla")
        print(driver.execute_script('return arguments[0].childNodes[11].textContent;', WebDriverWait(driver, 10).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "div.PageBox")))).strip())
        
      • 控制台输出:

        Classification of the genus Acacia (in the wider sense) has been subject to considerable debate. It is generally agreed that there are valid reasons for breaking it up into several distinct genera, but there has been disagreement over the way this should be done. As of 2017, it is widely (but not completely) accepted that the section that includes the majority of the Australian species (including this one) should retain the name Acacia, whilst other sections of the genus should be transferred to the genera Acaciella, Mariosousa, Senegalia and Vachellia[
        

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-08-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2022-01-02
        • 1970-01-01
        • 1970-01-01
        • 2020-05-22
        相关资源
        最近更新 更多