簡體   English   中英

在使用 Selenium Chromedriver 和 Python 時需要幫助

[英]Need help using Selenium Chromedriver and Python

我想在頁面的“他的”價格旁邊打印每個商家名稱,如下所示:

Climaconvenienza 1.031,79 €

Hwonline 1.031,80 €

Shopdigit 1.073,90 €

我制作的代碼是這樣的:

browser.get('https://www.trovaprezzi.it/televisori-lcd-plasma/prezzi-scheda-prodotto/lg_oled_cx3?sort=prezzo_totale')
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".merchant_name_and_logo img")))
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".merchant_name_and_logo img"))) 

names = browser.find_elements_by_css_selector(".merchant_name_and_logo img")

for span in names:
    print(span.get_attribute("alt"))
    
all_divs = browser.find_elements_by_xpath("//div[@class='item_total_price']")
for div in all_divs:
    print(div.text)

但是,通過運行我的代碼,我得到了這個:

氣候便利

在線

店鋪數字

1.031,79 歐元

1.031,80 歐元

1.073,90 歐元

假設namesall_divs始終具有相同的長度(就像在您的示例中所做的那樣),以下應該有效:

browser.get('https://www.trovaprezzi.it/televisori-lcd-plasma/prezzi-scheda-prodotto/lg_oled_cx3?sort=prezzo_totale')
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".merchant_name_and_logo img")))
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".merchant_name_and_logo img"))) 

names = browser.find_elements_by_css_selector(".merchant_name_and_logo img")
all_divs = browser.find_elements_by_xpath("//div[@class='item_total_price']")

for i in range(len(names)):
    print(names[i].get_attribute("alt") + ' ' + all_divs[i].text)
    

這將打印賣家列表和價格。 您可以根據需要修改output。

from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.wait import WebDriverWait


browser = webdriver.Chrome(executable_path='/snap/bin/chromium.chromedriver')

browser.get('https://www.trovaprezzi.it/televisori-lcd-plasma/prezzi-scheda-prodotto/lg_oled_cx3?sort=prezzo_totale')
wait = WebDriverWait(browser, 10)
wait.until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, ".merchant_name_and_logo img")))

listings = browser.find_elements_by_css_selector(".listing_item.clearfix")

result = []
for listing in listings:
    name = listing.find_element_by_css_selector(".merchant_name_and_logo img").get_attribute("alt")
    price = listing.find_element_by_css_selector(".item_total_price").text
    result.append([name, price])

print(*result, sep='\n')

我的 output:

['Climaconvenienza', 'Tot. 1.031,79 €']
['Hwonline', 'Tot. 1.031,80 €']
['Shopdigit', 'Tot. 1.073,90 €']
['eBay', 'Tot. 1.085,00 €']
['ePrice', 'Tot. 1.123,99 €']
['Onlinestore', 'Tot. 1.124,80 €']
['Hwonline', 'Tot. 1.129,90 €']
['Shoppyssimo', 'Tot. 1.148,00 €']
['Prezzo forte', 'Tot. 1.166,39 €']
['eBay', 'Tot. 1.181,26 €']
['eBay', 'Tot. 1.193,42 €']
['eBay', 'Tot. 1.199,00 €']
['eBay', 'Tot. 1.244,91 €']
['eBay', 'Tot. 1.249,00 €']
['eBay', 'Tot. 1.269,62 €']
['Yeppon', 'Tot. 1.288,89 €']
['Showprice', 'Tot. 1.301,80 €']
['Galagross', 'Tot. 1.419,99 €']
['Climaconvenienza', 'Tot. 1.519,17 €']
['Di Lella Shop', 'Tot. 1.519,17 €']

要不帶括號打印,請使用:

for item in result:
  print(item[0], ', '.join(map(str, item[1:])))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM