gpt4 book ai didi

python - 如何从航类预订网站 https ://reservations. airarabia.com 获取价格信息

转载 作者:太空宇宙 更新时间:2023-11-04 04:03:50 26 4
gpt4 key购买 nike

我是 python 的新手,正在尝试学习网络抓取。按照教程,我试图从网站上提取价格,但没有打印任何内容。我的代码有什么问题?

from selenium import webdriver

chrome_path = r"C:\webdrivers\chromedriver.exe"
driver = webdriver.Chrome(chrome_path)
driver.get("https://reservations.airarabia.com/service-app/ibe/reservation.html#/fare/en/AED/AE/SHJ/KHI/07-09-2019/N/1/0/0/Y//N/N")
price = driver.find_elements_by_class_name("fare-and-services-flight-select-fare-value ng-isolate-scope")
for post in price:
print(post.text)

最佳答案

要打印第一个 title,您必须为所需的 visibility_of_element_located() 引入 WebDriverWait,您可以使用以下任一 Locator Strategies:

  • 使用 CSS_SELECTOR:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.CSS_SELECTOR, "isa-flight-select button:first-child span.fare-and-services-flight-select-fare-value.ng-isolate-scope"))).get_attribute("innerHTML"))
  • 使用 XPATH:

    print(WebDriverWait(driver, 20).until(EC.visibility_of_element_located((By.XPATH, "//isa-flight-select//following::button[contains(@class, 'button')]//span[@class='fare-and-services-flight-select-fare-value ng-isolate-scope']"))).text)
  • 注意:您必须添加以下导入:

    from selenium.webdriver.support.ui import WebDriverWait
    from selenium.webdriver.common.by import By
    from selenium.webdriver.support import expected_conditions as EC
  • 两次背靠背执行的控制台输出:

    475

You can find a relevant discussion in How to retrieve the title attribute through Selenium using Python?


结尾

根据文档:

关于python - 如何从航类预订网站 https ://reservations. airarabia.com 获取价格信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57739736/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com