代码之家  ›  专栏  ›  技术社区  ›  yash agarwal

Python Selenium-如何基于span标记内的文本提取元素?

  •  0
  • yash agarwal  · 技术社区  · 2 年前

    我正在从URL中提取一些数据 https://blinkit.com/prn/catch-cumin-seedsjeera-whole/prid/56692 具有非结构化的产品细节元素。

    使用此代码:

     product_details = wd.find_elements(by=By.XPATH, value="//div[@class='ProductAttribute__ProductAttributesDescription-sc-dyoysr-2 lnLDYa']")
     info_shelf_life = product_details[0].text.strip()
     info_country_of_origin = product_details[1].text.strip()
    

    正如您所见,产品详细信息元素是非结构化的,当索引从一个URL更改为另一个URL时,这种方法不适用

    因此尝试了这种方法,抛出了一个NoSuchWindowException错误。

    info_shelf_life = wd.find_element(By.XPATH,value= "//div[[contains(@class, 'ProductAttribute__ProductAttributesDescription-sc-dyoysr-2 lnLDYa') and contains(., 'Shelf Life')]/..")
    print(info_shelf_life.text.strip())
    

    如何根据span标记内的文本提取div内的文本?

    1 回复  |  直到 2 年前
        1
  •  1
  •   JaSON    2 年前

    您的XPath无效。你可以试试

    info_shelf_life = wd.find_element(By.XPATH, '//p[span="Shelf Life"]/following-sibling::div').text
    info_country_of_origin = wd.find_element(By.XPATH, '//p[span="Country of Origin"]/following-sibling::div').text
    

    获取所需数据