如何使用Shadow-root网站中的产品元素加速?

发布于 2025-01-31 03:23:35 字数 1254 浏览 4 评论 0原文

我是Python的新手,我正在尝试学习。我目前正在一个具有阴影根部的网站上进行网络刮擦项目,但是我在访问Shadow-root下的元素方面遇到了困难。我真的很感谢您的帮助和一些建议。该程序为“ untitled0.py”。另外,我试图仅使用Selenium Webdriver,而作为最后一个度假胜地的Shadowpy。无论我做什么,product_name_tos返回一个空列表。

import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
service = Service(executable_path=ChromeDriverManager().install())
driver_TOS = webdriver.Chrome(service=service)
driver_TOS.get("https://www.bedbathandbeyond.com/store/category/college/decor/10625?icid=hp_homepage_4acrs_slot2_bath")
prod_names_TOS = []
prod_prices_TOS = []
time.sleep(5)
shadow_host_TOS = driver_TOS.find_element("id", "wmHostPrimary")
shadow_root = driver_TOS.execute_script('return arguments[0].shadowRoot', shadow_host_TOS)
#shadow_root_TOS = shadow_host_TOS.shadowRoot
products_TOS = shadow_root.find_elements("css selector", ".prodCardWrap")
for product_TOS in products_TOS:
    product_container_TOS = product_TOS.find_element("class name", "prodCardR")
    product_name_TOS = product_container_TOS.find_element("class name", "prodTitle").get_element("innerText")
    prod_names_TOS.append(product_name_TOS)
print(prod_names_TOS)

I am new to python and I am trying to learn. I am currently doing a web scraping project on a website that has a shadow-root, but I have trouble accessing the elements under the shadow-root. I would really appreciate a bit of help and some advice. The program is 'untitled0.py'. Also, I am trying to only use selenium webdriver, and as a last resort shadowpy. Whatever I did, product_name_TOS returned an empty list.

import pandas as pd
from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from webdriver_manager.chrome import ChromeDriverManager
service = Service(executable_path=ChromeDriverManager().install())
driver_TOS = webdriver.Chrome(service=service)
driver_TOS.get("https://www.bedbathandbeyond.com/store/category/college/decor/10625?icid=hp_homepage_4acrs_slot2_bath")
prod_names_TOS = []
prod_prices_TOS = []
time.sleep(5)
shadow_host_TOS = driver_TOS.find_element("id", "wmHostPrimary")
shadow_root = driver_TOS.execute_script('return arguments[0].shadowRoot', shadow_host_TOS)
#shadow_root_TOS = shadow_host_TOS.shadowRoot
products_TOS = shadow_root.find_elements("css selector", ".prodCardWrap")
for product_TOS in products_TOS:
    product_container_TOS = product_TOS.find_element("class name", "prodCardR")
    product_name_TOS = product_container_TOS.find_element("class name", "prodTitle").get_element("innerText")
    prod_names_TOS.append(product_name_TOS)
print(prod_names_TOS)

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

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

发布评论

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

评论(1

半﹌身腐败 2025-02-07 03:23:35

几乎所有内容都是正确的,该方法中只有一个小错误

product_name_TOS = product_container_TOS.find_element("class name", "prodTitle").get_element("innerText")

.get_element,而是.get_attribute。请注意,在这种情况下,您也可以使用.text

Almost everything is correct, there is just a small error in

product_name_TOS = product_container_TOS.find_element("class name", "prodTitle").get_element("innerText")

The method is not .get_element but .get_attribute. Notice that in this case you can also use .text

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