gpt4 book ai didi

python - 如何获取 python selenium 中的所有数据?

转载 作者:太空宇宙 更新时间:2023-11-03 20:40:02 25 4
gpt4 key购买 nike

我无法从该网站获取所有数据,它只是多次向我提供相同的数据。

我尝试使用自定义 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()

最佳答案

您必须通过在 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)

关于python - 如何获取 python selenium 中的所有数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56916093/

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