python selenium-如何根据跨度标签中的文本提取元素?

发布于 2025-02-13 14:44:17 字数 986 浏览 2 评论 0原文

我正在从url https://blinkit.com/prn com.com/prn-prn-prn-prn-prn/prn-prn/prn-prn/prn-prn/prn-com/prn-prn/prn-com/prn-com-com/prn一下,我正在提取一些数据/catch-cumin-seedsjeera-hole/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())

如何根据跨度标签内部的文本在div中提取文本?

I am extracting some data from URLhttps://blinkit.com/prn/catch-cumin-seedsjeera-whole/prid/56692 with unstructured Product Details elements.

Using this code:

 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()

As you can see the Product details elements are unstructured and this approach is not suitable when the Index gets changed from URL to URL

Hence tried this approach, which throws out a NoSuchWindowException error.

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())

How can I extract text inside div based on text inside span tags?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

小嗷兮 2025-02-20 14:44:17

您的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

获取所需的数据

Your XPath is invalid. You can try

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

to get required data

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文