【问题标题】:Need help using Selenium Chromedriver and Python在使用 Selenium Chromedriver 和 Python 时需要帮助
【发布时间】:2021-05-30 19:32:25
【问题描述】:

我想在页面的“他的”价格旁边打印每个商家名称,如下所示:

Climaconvenienza 1.031,79 €

在线 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)

但是,通过运行我的代码,我得到了:

气候便利

Hwonline

店铺数字

1.031,79 欧元

1.031,80 欧元

1.073,90 欧元

【问题讨论】:

    标签: python selenium google-chrome selenium-webdriver selenium-chromedriver


    【解决方案1】:

    假设 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)
        
    

    【讨论】:

      【解决方案2】:

      这将打印卖家列表和价格。您可以根据需要修改输出。

      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')
      

      我的输出:

      ['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:])))
      

      【讨论】:

      • 不错的答案,你的 for 循环比我的更有意义 :)
      • @C.Peck 谢谢,它最初打印列表列表,我添加了一个关于如何删除这些内括号的解决方案。应该有更漂亮的。
      • 不错的解决方案,比我们昨天的做法领先一步 :)
      猜你喜欢
      • 2021-07-23
      • 1970-01-01
      • 1970-01-01
      • 2019-11-23
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 2018-10-21
      相关资源
      最近更新 更多