简体   繁体   English

如何使用 Selenium Python 从网站提取图书价格

[英]How to extract the price of the book from website using Selenium Python

I am trying to get the price of this book from this link (I am using google colab for the project)我正在尝试从此链接获取本书的价格(我正在为该项目使用 google colab)

link of the webpage网页链接

This is the code I have made:这是我所做的代码:

import sys
sys.path.insert(0,'/usr/lib/chromium-browser/chromedriver')
from selenium import webdriver
from selenium.webdriver.common.by import By
options = webdriver.ChromeOptions()
options.add_argument('--headless')
options.add_argument('--no-sandbox')
options.add_argument('--disable-dev-shm-usage')
wd = webdriver.Chrome('chromedriver',options=options)
wd.get("https://www.amazon.fr/dp/000101742X")
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

product_title = wd.find_element(By.CLASS_NAME, 'a-size-extra-large')

print(product_title.text)

product_image_url = wd.find_element(By.ID, 'imgBlkFront')

print(product_image_url.get_attribute('src'))

product_price = wd.find_element(By.CLASS_NAME, 'a-size-base a-color-price a-color-price')

print(product_price.text)

When I run the code, This is giving me an error当我运行代码时,这给了我一个错误

NoSuchElementException: Message: no such element: Unable to locate element: {"method":"css selector","selector":".a-size-base a-color-price a-color-price"}
  (Session info: headless chrome=99.0.4844.84)
Stacktrace:
#0 0x561c75bf5b63 <unknown>
#1 0x561c758ebc93 <unknown>
#2 0x561c75921ba0 <unknown>
#3 0x561c75921dc1 <unknown>
#4 0x561c75956267 <unknown>
#5 0x561c7593f33d <unknown>
#6 0x561c75953fac <unknown>
#7 0x561c7593f683 <unknown>
#8 0x561c75915c7c <unknown>
#9 0x561c75917145 <unknown>
#10 0x561c75c19fe0 <unknown>
#11 0x561c75c2b17f <unknown>
#12 0x561c75c2af19 <unknown>
#13 0x561c75c2b6e2 <unknown>
#14 0x561c75c643cb <unknown>
#15 0x561c75c2b941 <unknown>
#16 0x561c75c0ed13 <unknown>
#17 0x561c75c35098 <unknown>
#18 0x561c75c3522a <unknown>
#19 0x561c75c4e711 <unknown>
#20 0x7f37b1a316db <unknown>

I searched about it and I also tried some other methods我搜索了一下,也尝试了一些其他方法

wd.implicitly_wait(20) 
product_price = wd.find_element(By.CLASS_NAME, 'a-size-base a-color-price a-color-price')

print(product_price.text)
###########################################################
try:
    product_price = WebDriverWait(wd, 20).until(
        EC.presence_of_element_located((By.CLASS_NAME, 'a-size-base a-color-price a-color-price'))
    ) # wd.find_element(By.CLASS_NAME, 'a-size-base a-color-price a-color-price')
    print(product_price.text)

finally:
    wd.quit()
#################################################

button = wd.find_element(By.CLASS_NAME, 'a-button a-button-selected a-spacing-mini a-button-toggle format')
button.click()
product_price = wd.find_element(By.CLASS_NAME, 'a-size-base a-color-price a-color-price')

print(product_price.text)

All of these methods gave me errors.所有这些方法都给了我错误。 Can someone help?有人可以帮忙吗? Thanks.谢谢。

To print the price text ie 23,72 you need to induce WebDriverWait for the visibility_of_element_located() and you can use either of the following locator strategies :要打印价格文本,即23,72 ,您需要为visibility_of_element_located()引入WebDriverWait ,您可以使用以下任一定位器策略

  • Using XPATH and text attribute:使用XPATH文本属性:

     driver.get("https://www.amazon.fr/dp/000101742X") WebDriverWait(driver, 20).until(EC.element_to_be_clickable((By.XPATH, "//input[@name='accept']"))).click() print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//span[text()='Partition']//following::span[1]"))).text.split(" ")[3]) driver.quit()
  • Console Output:控制台 Output:

     23,72
  • Note : You have to add the following imports:注意:您必须添加以下导入:

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

You can find a relevant discussion in How to retrieve the text of a WebElement using Selenium - Python您可以在How to retrieve the text of a WebElement using Selenium - Python中找到相关讨论

声明:本站的技术帖子网页,遵循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 如何使用 Selenium 和 Python 提取小计价格 - How to extract the Subtotal Price using Selenium and Python 如何通过Python Selenium BeautifulSoup从网站上提取安全价格作为文本 - How to extract the price for the security as text from the website through Python Selenium BeautifulSoup 如何在python中使用硒从网站提取openload链接 - how to extract openload link from a website using selenium in python 使用Selenium Webdriver(Python)从网站提取图像 - Extract image from website using Selenium Webdriver (Python) 如何使用 python selenium 从 aws 网站提取 ec2 实例详细信息,例如名称、memory - How to extract ec2 instance details like name, memory from aws website using python selenium 如何使用 python、selenium 和 chromedriver 从网站中提取此值 - how can i extract this value from a website, with python, selenium and chromedriver 如何使用 python 从网站中提取表格 - How to extract table from website using python 如何使用 python 从网站中提取表格? - How to extract a table from the website using python? 如何在python中使用正则表达式从html提取价格 - How to extract the price from html using regex in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM