简体   繁体   English

如何从不同尺寸的硒 python 中获取产品价格

[英]how to get product price from different size selenium python

I want to scrape product information from this page .我想从这个页面抓取产品信息。 This product have three different size and price will be change if I select different size from drop-down section.此产品有三种不同的尺寸,如果我从下拉部分选择不同的尺寸,价格会有所变化。 Right now my scraper only can scrape default price after first time initially page load which is 35 for 1kg.现在我的刮板只能在第一次初始页面加载后刮默认价格,即 35 公斤。 How I will scrape price for 500g and 250g.我将如何刮 500g 和 250g 的价格。 here is my code:这是我的代码:

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.common.by import By

#argument for incognito Chrome
#argument for incognito Chrome
option = Options()
option.add_argument("--incognito")


browser = webdriver.Chrome(options=option)
browser.get("https://boutique.cafebarista.ca/products/cremone?variant=18033418797121")

product_title = browser.find_element_by_xpath('//h1[@class="product-name"]')
long_description = browser.find_element_by_xpath('//div[@class="product-landing-container"]')
price=browser.find_element_by_xpath('//div[@class="product-btn-price ProductPrice"]')

print(product_title.text,long_description.text,price.text)


browser.quit()

With .find_elements_by_css_selector you can get each text without clicking the weight drop down first, this is the selector I mean:使用.find_elements_by_css_selector您无需先单击weight下拉菜单即可获取每个文本,这是我的意思的选择器:

nav[id="w-dropdown-list-16"] > a > div

And you can also click on each of these elements using .execute_script您还可以使用.execute_script单击这些元素中的每一个

Try following code:尝试以下代码:

driver.get('https://boutique.cafebarista.ca/products/autentico?variant=18033459331137')
weight_list = driver.find_elements_by_css_selector('nav[id="w-dropdown-list-16"] > a > div')
for weight in weight_list:
    driver.execute_script('arguments[0].click();', weight)
    price = driver.find_element_by_id('ProductPrice').text
    print(weight.get_attribute('innerHTML') +' ' +price)

Try below solution尝试以下解决方案

from selenium.webdriver.common.action_chains import ActionChains
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium import webdriver

browser = webdriver.Chrome(executable_path=r"C:\New folder\chromedriver.exe")
browser.maximize_window()
wait = WebDriverWait(browser, 20)

browser.get("https://boutique.cafebarista.ca/products/cremone?variant=18033418797121")

kg_button=browser.find_element_by_xpath("//div[@id='w-dropdown-toggle-16']")
kg_button.click()
list =wait.until(EC.presence_of_all_elements_located((By.XPATH, "//nav[@id='w-dropdown-list-16']//a")))
kg_button.click()
for element in list:
     kg_button.click()
     actionChains = ActionChains(browser)
     actionChains.move_to_element(element).click().perform()

     price = browser.find_element_by_xpath("//div[@id='ProductPrice']")
     print product_title.text
     print element.text
     print price.text


browser.quit()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何使用 Selenium 和 Python 从网站以数字形式获取价格 - How to get the price as a number from a website using Selenium and Python 如何使用Python Amazon Simple Product API来获取产品的价格 - How to use Python Amazon Simple Product API to get price of a product 在python中使用宽瓶从不同地区的亚马逊提取产品价格 - using bottlenose in python to extract product price from Amazon in different locale 如何从 selenium python 中的相同 class 获取不同的数据? - How to get different data from same class in selenium python? 如何通过Selenium和Python从HTML DOM获得不同的文本 - How to get different texts from the HTML DOM through Selenium and Python 如何使用python-amazon-product-api item_lookup函数获取亚马逊物品的价格优惠清单? - How to get the list of price offers on an item from Amazon with python-amazon-product-api item_lookup function? 为什么我无法在 python 中使用 selenium 显示产品价格? - Why am I not able to display the product price using selenium in python? 从odoo的订单行获取产品价格? - get product price from order line in odoo? 使用 python 和 selenium 从网页获取产品详细信息 - Get product details from a webpage using python and selenium 如何在 Django (Python) 中对产品进行价格排序? - How to do price sorting of product in Django (Python)?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM