简体   繁体   English

Python Selenium - 无法从网页检索价格

[英]Python Selenium - Can't manage to retrieve price from webpage

I am trying to retrieve a price from a product on a webshop but can't find the right code to get it.我正在尝试从网上商店的产品中检索价格,但找不到正确的代码来获取它。

Price of product I want to extract: https://www.berger-camping.nl/zoeken/?q=3138522088064我要提取的产品价格: https ://www.berger-camping.nl/zoeken/?q=3138522088064

This is the line of code i have to (try) and retrieve the price:这是我必须(尝试)并检索价格的代码行:

Prijs_BergerCamping = driver.find_element(by=By.XPATH, value='//div[@class="prod_price__prod_price"]').text
        print(Prijs_BergerCamping)

Any tips on what I seem to be missing?关于我似乎缺少的任何提示?

Your code is correct.你的代码是正确的。
I guess all you missing is to wait for element visibility.我想你所缺少的只是等待元素可见性。
This code works:此代码有效:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)

url = "https://www.berger-camping.nl/zoeken/?q=3138522088064"
driver.get(url)

price = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class="prod_price__prod_price"]'))).text
print(price)

The output is:输出是:

79,99 €

But you also need to close the cookies banner and Select store dialog (at least I see it).但您还需要关闭 cookies 横幅和选择商店对话框(至少我看到了)。 So, my code has the following additionals:因此,我的代码具有以下附加功能:

from selenium import webdriver
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.chrome.options import Options
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

options = Options()
options.add_argument("start-maximized")

webdriver_service = Service('C:\webdrivers\chromedriver.exe')
driver = webdriver.Chrome(options=options, service=webdriver_service)
wait = WebDriverWait(driver, 20)

url = "https://www.berger-camping.nl/zoeken/?q=3138522088064"
driver.get(url)

wait.until(EC.element_to_be_clickable((By.ID, "onetrust-accept-btn-handler"))).click()
wait.until(EC.element_to_be_clickable((By.XPATH, "//div[@style] //*[contains(@class,'uk-close')]"))).click()
price = wait.until(EC.visibility_of_element_located((By.XPATH, '//div[@class="prod_price__prod_price"]'))).text
print(price)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM