我是 python 新手,我正在努力學習。我目前正在一個具有 shadow-root 的網站上進行網路抓取專案,但我無法訪問 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)
uj5u.com熱心網友回復:
幾乎所有內容都是正確的,只是有一個小錯誤
product_name_TOS = product_container_TOS.find_element("class name", "prodTitle").get_element("innerText")
方法不是.get_element但是.get_attribute。請注意,在這種情況下,您還可以使用.text
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/480108.html
上一篇:使用python從網站中提取資訊
