我有一個簡單的問題,假設可以很容易地解決。然而,我現在用了一些時間來提取四行資訊,如下所示:
在此處查看 html 結構
我首先嘗試訪問<ul _ngcontent-xl-byg-c79="" 專案,然后遍歷<li _ngcontent-xl-byg-c79="" >專案以將嵌入的資訊存盤在我的資料框中(列是“M?rke”、“Produkttype”、“Serie”和“Model”)。
我做錯了什么?我的問題是這四行具有相同的“類”名稱,這在所有四個回圈中為我提供了相同的輸出。
這是我的代碼:
from selenium import webdriver
import pandas as pd
# Activate web browser: External control
browser = webdriver.Chrome(r'C:\Users\KristerJens\Downloads\chromedriver_win32\chromedriver')
# Get webpage
browser.get("https://www.xl-byg.dk/shop/knauf-insulation-ecobatt-murfilt-190-mm-2255993")
# Get information
brand= []
product= []
series=[]
model=[]
for i in browser.find_elements_by_xpath("//ul[@class='short ng-star-inserted']/li"):
for p in i.find_elements_by_xpath("//span[@class='attribute-name']"):
brand.append(i.find_elements_by_class_name('?').text)
product.append(i.find_elements_by_class_name('?').text)
series.append(i.find_elements_by_class_name('?').text)
model.append(i.find_elements_by_class_name('?').text)
df = pd.DataFrame()
df['brand'] = brand
df['product'] = product
df['series'] = series
df['model'] = model
非常感謝任何幫助!
uj5u.com熱心網友回復:
嘗試如下并確認:
from selenium import webdriver
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver = webdriver.Chrome(executable_path="path to chromedriver.exe")
driver.maximize_window()
driver.implicitly_wait(10)
driver.get("https://www.xl-byg.dk/shop/knauf-insulation-ecobatt-murfilt-190-mm-2255993")
wait = WebDriverWait(driver,30)
# Cookie pop-up
wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@aria-label='Accept all' or @aria-label = 'Accepter alle']"))).click()
options = driver.find_elements_by_xpath("//div[@class='row-column']//ul[contains(@class,'short')]/li")
for opt in options:
attribute = opt.find_element_by_xpath("./span[@class='attribute-name']").text # Use a "." in the xpath to find element within in an element
value = opt.find_element_by_xpath("./*[contains(@class,'ng-star-inserted')]").text
print(f"{attribute} : {value}")
M?rke : Knauf Insulation
Produkttype : Murfilt
Serie : ECOBATT
Materiale : Glasmineraluld
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329938.html
