我无法从该站点获取所有数据,只是多次给我相同的数据。
我尝试使用自定义xpath,但是它多次给我单个数据。
import time
import selenium
from selenium import webdriver
browser = webdriver.Chrome()
browser.get("https://www.spicejet.com/")
departureButton =
browser.find_element_by_id("ctl00_mainContent_ddl_originStation1_CTXT")
departureButton.click()
browser.find_element_by_partial_link_text("Kolkata").click()
arivalButton = browser.find_element_by_id("ctl00_mainContent_ddl_destinationStation1_CTXT")
arivalButton.click()
browser.find_element_by_partial_link_text("Goa").click()
date_position =
browser.find_element_by_id("ctl00_mainContent_view_date1")
date_position.click()
search_date = "20-September 2019"
dep_date = search_date.split("-")
dep_month = dep_date[1]
dep_day = dep_date[0]
while browser.find_element_by_class_name("ui-datepicker-title").text != dep_month:
browser.find_element_by_css_selector("a[data-handler='next']").click()
browser.find_element_by_xpath("//table//a[text()='"+dep_day+"']").click()
time.sleep(1)
pax_selct = browser.find_element_by_id("divpaxinfo").click()
time.sleep(.2)
# adult number
for i in range(0, 1 - 1):
adults = browser.find_element_by_id("hrefIncAdt")
adults.click()
# child number
for i in range(0, 1):
childrens = browser.find_element_by_id("hrefIncChd")
childrens.click()
# infant number
for i in range(0, 1):
infants = browser.find_element_by_id("hrefIncInf")
infants.click()
donebttn = browser.find_element_by_id("btnclosepaxoption").click()
searchBtn =
browser.find_element_by_id("ctl00_mainContent_btn_FindFlights").click()
browser.switch_to.default_content()
flightarr = []
tbl_row = browser.find_elements_by_class_name("fare-row")
for item in tbl_row:
if item.is_displayed():
col = item
flightinfo = {}
flightNo = col.find_element_by_class_name("special").text
depTime = col.find_element_by_class_name("departure-time").text
trvlDuration = col.find_element_by_class_name("travel-duration").text
arrv_time = col.find_element_by_class_name("hide-below-480").text
price1 = item.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr/td[3]').text
price2 = item.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr/td[4]').text
price3 = item.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr/td[5]').text
price4 = item.find_element_by_xpath('//*[@id="availabilityTable0"]/tbody/tr/td[6]').text
flightinfo["flight_number"] = flightNo
flightinfo["depart_time"] = depTime
flightinfo["flight_duration"] = trvlDuration
flightinfo["arrival_time"] = arrv_time
flightinfo["i0"] = price1
flightinfo["i1"] = price2
flightinfo["i2"] = price3
flightinfo["i3"] = price4
flightarr.append(flightinfo)
print(flightarr)
time.sleep(2)
# browser.close()
答案 0 :(得分:0)
您必须通过在xpath中添加.
将for循环中的范围限制为当前项。
这是您应该使用的脚本。
for item in tbl_row:
if item.is_displayed():
col = item
flightinfo = {}
flightNo = col.find_element_by_xpath(".//*[@class='special']").text
depTime = col.find_element_by_xpath(".//*[@class='departure-time']").text
trvlDuration = col.find_element_by_xpath(".//*[@class='travel-duration']").text
arrv_time = col.find_element_by_xpath(".//*[@class='hide-below-480']").text
price1 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[3]').text
price2 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[4]').text
price3 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[5]').text
price4 = item.find_element_by_xpath('.//*[@id="availabilityTable0"]/tbody/tr/td[6]').text
flightinfo["flight_number"] = flightNo
flightinfo["depart_time"] = depTime
flightinfo["flight_duration"] = trvlDuration
flightinfo["arrival_time"] = arrv_time
flightinfo["i0"] = price1
flightinfo["i1"] = price2
flightinfo["i2"] = price3
flightinfo["i3"] = price4
flightarr.append(flightinfo)
print(flightarr)