如何使用Selenium和Python刮所有的价格

mpgws1up  于 2023-01-26  发布在  Python
关注(0)|答案(2)|浏览(157)

我试图从Viagogo的票价没有运气。脚本似乎很简单,并与其他网站,但不是Viagogo的作品。我没有问题,在标题从这里得到的文本。
它总是返回一个空的结果(即[])。有人能帮我吗?
代码试验:

from selenium import webdriver
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.chrome.service import Service
from selenium.webdriver.common.by import By
import pandas as pd

s = Service("[]/Downloads/chromedriver/chromedriver.exe")
driver = webdriver.Chrome(service=s)
driver.get('https://www.viagogo.com/Concert-Tickets/Country-and-Folk/Taylor-Swift-Tickets/E-151214704')
price = driver.find_elements(by=By.XPATH, value('//div[@id="clientgridtable"]/div[2]/div/div/div[3]/div/div[1]/span[@class="t-b fs16"]'))
Print(price)
[]

我期待的7个价格定义在右手边的网站位于略高于"每张票"

dsekswqp

dsekswqp1#

price的定义中有错误,它应该是value='...'而不是value('...')。此外,您应该使用wait命令定义它,以便驱动程序等待价格在页面上显示。

price = WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "...")))

请注意,此命令需要以下导入

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
vnzz0bqm

vnzz0bqm2#

要提取website中右侧定义的所有7个价格,您必须为visibility_of_all_elements_located()导出WebDriverWait,您可以使用以下locator strategies之一:

  • 使用 * CSS_SELECTOR * 和 * 文本 * 属性:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.CSS_SELECTOR, "div.f-list__cell-pricing-ticketstyle > div.w100 > span")))])
  • 使用 * XPATH * 和 * get_attribute("innerHTML") *:
print([my_elem.text for my_elem in WebDriverWait(driver, 20).until(EC.visibility_of_all_elements_located((By.XPATH, "//div[@class='f-list__cell-pricing-ticketstyle']/div[@class='w100 ']/span")))])
  • 控制台输出:
['Rs.25,509', 'Rs.25,873', 'Rs.27,403', 'Rs.28,788', 'Rs.72,809', 'Rs.65,593', 'Rs.29,153', 'Rs.29,153']
      • 注意**:您必须添加以下导入:
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC

相关问题