如何使用Shadow-root网站中的产品元素加速?
我是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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
几乎所有内容都是正确的,该方法中只有一个小错误
是
.get_element
,而是.get_attribute
。请注意,在这种情况下,您也可以使用.text
Almost everything is correct, there is just a small error in
The method is not
.get_element
but.get_attribute
. Notice that in this case you can also use.text