我真的很难通过以下使用硒的 xpath 从 kayak 中降低机票价格。当我运行“pries”时,它只返回一个空白列表。以下是我正在使用的
from time import sleep, strftime
from random import randint
import pandas as pd
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
import smtplib
chromedriver_path = 'C:/Users/Documents/chromedriver.exe'
driver = webdriver.Chrome(executable_path=chromedriver_path)
kayak = ('https://www.kayak.com/flights/JFK-SFO/2021-12-25/2022-01-03?sort=bestflight_a')
driver.get(kayak)
sleep(randint(10,20))
xp_prices = '//a[@class="booking-link whisky-booking-link"]/span[@class="price option-text"]'
prices = driver.find_elements_by_xpath(xp_prices)
print(prices)
答案 0 :(得分:0)
预订链接中的简单 _,不要使用睡眠,而是使用 webdriver 等待。
wait = WebDriverWait(driver, 10)
kayak = 'https://www.kayak.com/flights/JFK-SFO/2021-12-25/2022-01-03?sort=bestflight_a'
driver.get(kayak)
wait.until(EC.element_to_be_clickable((By.XPATH,'/html/body/div[46]/div[3]/div/button'))).click()
xp_prices = '//a[@class="booking-link "]/span[@class="price option-text"]'
prices = wait.until(EC.presence_of_all_elements_located((By.XPATH,xp_prices)))
for price in prices:
print(price.text)
导入
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
输出
$281
$302
$285
$289
$293
$302
$302
$306
$306
$306
$309
$313
$326
$346
$348