我設法從此頁面上的產品中提取了名稱、規格、價格和價格單位:https ://www.bauhaus.info/baustoffe/c/10000819 。
但是,我只能設法在頁面上顯示前 36 種產品。當按下“更多專案”按鈕時,我將如何提取此頁面上出現的所有產品?
為此,請參閱此處的頁面檢查:
在這里查看檢查
很感謝任何形式的幫助!
這是我的代碼:
from selenium import webdriver
import pandas as pd
import re
browser = webdriver.Chrome(r'C:\Users\KristerJens\Downloads\chromedriver_win32\chromedriver')
browser.get('https://www.bauhaus.info/baustoffe/c/10000819')
names= []
specs = []
prices = []
priceUnit = []
for li in browser.find_elements_by_xpath("//ul[@class='product-list-tiles row list-unstyled']/li"):
names.append(li.find_element_by_class_name("product-list-tile__info__name").text)
specs.append(li.find_element_by_class_name("product-list-tile__info__attributes").text)
prices.append(li.find_element_by_class_name("price-tag__box").text.split('\n')[0] "€")
p = li.find_element_by_class_name("price-tag__sales-unit").text.split('\n')[0]
priceUnit.append(p[p.find("(") 1:p.find(")")])
df2 = pd.DataFrame()
df2['names'] = names
df2['specs'] = specs
df2['prices'] = prices
df2['priceUnit'] = priceUnit
uj5u.com熱心網友回復:
首先嘗試單擊“更多產品”按鈕,直到它被禁用,即所有產品都被列出,然后使用通用 xpath 來定位產品資訊。
uj5u.com熱心網友回復:
對于每個頁面添加滾動到元素更多專案并單擊它,請參閱下面的滾動到元素實作示例
from selenium.webdriver.common.action_chains import ActionChains
element = driver.find_element_by_id("more_items")
actions = ActionChains(driver)
actions.move_to_element(element).perform()
uj5u.com熱心網友回復:
可以More option使用以下代碼連續點擊。嘗試將其與您的代碼合并。
# Imports Required for Explicit Waits:
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
driver.get("https://www.bauhaus.info/baustoffe/c/10000819")
wait = WebDriverWait(driver,30)
options = driver.find_elements_by_xpath("//ul[@class='product-list-tiles row list-unstyled']/li")
print(len(options))
#Using `Count` variable to keep track of number of times of clicking on More option. Remove the `Count` part of the code to continuously click on More option.
count = 0
try:
while True:
if count > 5: # Click on "More" option only for 5 times
break
moreoption = wait.until(EC.element_to_be_clickable((By.XPATH,"//button[@data-message='adb-show-more-products-button']")))
driver.execute_script("arguments[0].scrollIntoView(true);",moreoption)
driver.execute_script("window.scrollBy(0,-300);")
time.sleep(2)
moreoption.click()
count = 1
time.sleep(2)
options = driver.find_elements_by_xpath("//ul[@class='product-list-tiles row list-unstyled']/li")
print(len(options))
except:
pass
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/329946.html
