gpt4 book ai didi

python - 使用 Selenium 查找具有相同类的两个不同 div 中的不同元素

转载 作者:行者123 更新时间:2023-12-01 00:48:38 24 4
gpt4 key购买 nike

我正在使用 Python 和 Selenium 来抓取网站。使用 find_by_element 来查找我需要的所有值,但我遇到了更具挑战性的事情。网站 html 显示了两个不同值的确切结构,我无法使用简单的 find_element_by_class 因为它们具有相同的类和 id。我不想使用 xpath 或选择器,因为我正在通过许多“flight-row”div 进行迭代,这会使思考更加硬编码。

<div class="flight-row">
<div class="row row-eq-heights">
<div class="col-xs-4 col-md-4 no-padding"><span class="airline-name">gol</span><span class="flight-number">AM-477</span></div>
<div class="col-xs-4 col-md-4">
<div class="flight-timming"><span class="flight-time">06:15</span><span class="flight-destination">IAH</span></div><span class="flight-data">01/10/19</span></div>
<div class="col-xs-4 col-md-4 no-padding">
<div class="duration"><span class="flight-duration">21:25</span><span class="flight-stops" aria-label="Paradas do voo">2 paradas</span></div>
</div>
<div class="col-xs-4 col-md-4">
<div class="flight-timming"><span class="flight-destination">GIG</span><span class="flight-time">05:40</span></div><span class="flight-data">02/10/19</span></div>
</div>
</div>

我想从两个“col-xs-4 col-md-4”div 中获取飞行时间、飞行目的地和飞行数据的值。

这是我的一些代码:

outbound_flights = driver.find_elements_by_css_selector("div[class^='flight-item ']")

for outbound_flight in outbound_flights:
airline = outbound_flight.find_element_by_css_selector("span[class='airline-name']")

谢谢!

最佳答案

尝试使用以下 CSS 选择器来获取 flight-timeflight-destinationflight-data

outbound_flights = driver.find_elements_by_css_selector("div.col-xs-4.col-md-4:not(.no-padding)")

for outbound_flight in outbound_flights:
flight_time = outbound_flight.find_element_by_css_selector("div.flight-timming span.flight-time").text
print(flight_time)
flight_destination = outbound_flight.find_element_by_css_selector("div.flight-timming span.flight-destination").text
print(flight_destination)
flight_data = outbound_flight.find_element_by_css_selector("span.flight-data").text
print(flight_data)

控制台输出:

06:15
IAH
01/10/19
05:40
GIG
02/10/19

编辑答案:

outbound_flights = driver.find_elements_by_css_selector("div.col-xs-4.col-md-4:not(.no-padding)")
flighttime=[]
for outbound_flight in outbound_flights:
flight_time = outbound_flight.find_element_by_css_selector("div.flight-timming span.flight-time").text
print(flight_time)
flighttime.append(flight_time)
flight_destination = outbound_flight.find_element_by_css_selector("div.flight-timming span.flight-destination").text
print(flight_destination)
flight_data = outbound_flight.find_element_by_css_selector("span.flight-data").text
print(flight_data)

departure_time=flighttime[0]
arrival_time=flighttime[1]

print("Departure time :" + departure_time)
print("Arrival time :" + arrival_time)

关于python - 使用 Selenium 查找具有相同类的两个不同 div 中的不同元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56743599/

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