如何从航班预订网站https://reservations.airarabia.com获取价格信息 [英] How to grab the price information from flight reservation site https://reservations.airarabia.com
问题描述
我是python的新手,正在尝试学习网络抓取.在学习完一个教程之后,我试图从一个网站中提取价格,但没有打印任何内容.我的代码有什么问题?
I'm very new to python and trying to learn webscraping. Following a tutorial, I'm trying to extract a price from a website but nothing is being printed. What is wrong with my code?
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 ,您可以使用以下定位器策略:
To print the first title you have to induce WebDriverWait for the desired visibility_of_element_located()
and you can use either of the following 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)
注意:您必须添加以下导入:
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
控制台输出两个背对背执行:
Console Output of two back to back execution:
475
您可以在
Outro
根据文档:
Outro
As per the documentation:
get_attribute()
方法Gets the given attribute or property of the element.
text
属性返回The text of the element.
- 使用Selenium的文本和innerHTML之间的区别
get_attribute()
methodGets the given attribute or property of the element.
text
attribute returnsThe text of the element.
- Difference between text and innerHTML using Selenium
这篇关于如何从航班预订网站https://reservations.airarabia.com获取价格信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!