用硒刮擦:在字典中返回信息

2024-09-29 01:27:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在删除链接“https://www.kayak.it/flights/MIL-BCN/2021-09-01/2021-09-02/?sort=bestflight_a“在Python中使用Selenium

我想从字典中获取信息,输出如下:

[{'data_partenza': '2021-09-01', 'data_ritorno': '2021-09-02', 'price': '40', 'flights': '22:20 – 23:55\n'
             'BGY Bergamo Orio al Serio\n'
             '‐\n'
             'BCN Barcellona-El Prat\n'
             'diretto\n'
             '1h 35m\n'
             '6:20 – 8:00\n'
             'BCN Barcellona-El Prat\n'
             '‐\n'
             'BGY Bergamo Orio al Serio\n'
             'diretto\n'
             '1h 40m',
 {'data_partenza': '2021-09-01', 'data_ritorno': '2021-09-02', price: '34', 'flights': '16:35 – 18:05\n'
             'LIN Aeroporto Milano Linate\n'
             '‐\n'
             'BCN Barcellona-El Prat\n'
             'diretto\n'
             '1h 30m\n'
             '6:20 – 8:00\n'
             'BCN Barcellona-El Prat\n'
             '‐\n'
             'BGY Bergamo Orio al Serio\n'
             'diretto\n'
             '1h 40m'}, 
....]

但是我得到了这个输出:

[{'data_partenza': '2021-09-01',
  'data_ritorno': '2021-09-02',
  'flights': '22:20 – 23:55\n'
             'BGY Bergamo Orio al Serio\n'
             '‐\n'
             'BCN Barcellona-El Prat\n'
             'diretto\n'
             '1h 35m\n'
             '6:20 – 8:00\n'
             'BCN Barcellona-El Prat\n'
             '‐\n'
             'BGY Bergamo Orio al Serio\n'
             'diretto\n'
             '1h 40m',
  'price': None}]
[{'data_partenza': '2021-09-01',
  'data_ritorno': '2021-09-02',
  'flights': '22:20 – 23:55\n'
             'BGY Bergamo Orio al Serio\n'
             '‐\n'
             'BCN Barcellona-El Prat\n'
             'diretto\n'
             '1h 35m\n'
             '6:20 – 8:00\n'
             'BCN Barcellona-El Prat\n'
             '‐\n'
             'BGY Bergamo Orio al Serio\n'
             'diretto\n'
             '1h 40m',
  'price': None}, ...

所以我有两个问题:我有很多不同的字典,我只想要一本字典;此外,当我试图削价飞行时,我总是得到“无”

这是我的代码:

from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions as EC
import pprint

wd = webdriver.Chrome('chromedriver',chrome_options = chrome_options)
wd.maximize_window()
wd.implicitly_wait(50)
#driver.get("https://account.battle.net/creation/flow/creation-full")
wait = WebDriverWait(wd, 20)

link = 'https://www.kayak.it/flights/MIL-BCN/2021-09-01/2021-09-02/?sort=bestflight_a'
wd.get(link)
try:
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[title='Accetta']"))).click()
except:
    pass

try:
    wait.until(EC.element_to_be_clickable((By.CSS_SELECTOR, "button[id='onetrust-accept-btn-handler']"))).click()
except:
    pass

detail_flights = []
j = 0
lngth = len(wd.find_elements_by_css_selector(".mainInfo"))
for i in range(lngth):
    flights = ""
    data_partenza = ""
    data_ritorno = ""
    price = ""

    try:
        if len(wd.find_elements_by_css_selector(".mainInfo")) > 0:
            elements = wd.find_elements_by_css_selector(".mainInfo")
            wd.execute_script("arguments[0].scrollIntoView(true);", elements[j])
            #print(elements[j].get_attribute('innerText'))
            j = j + 1
            #detail_flights.append(elements[j].get_attribute('innerText'))
            date = link.replace('https://www.kayak.it/flights/', '')
            data_partenza = date[8:18]
            data_ritorno = date[19:29]

            detail_flights.append({'flights': elements[j].get_attribute('innerText'),
                                   'price': elements[j].get_attribute('SrON-mb'),
                                   'data_partenza': data_partenza,
                                   'data_ritorno': data_ritorno})
            pprint.pprint(detail_flights[0:])
        else:
            print('Nothing more to scrape')
    except:
        pass

Tags: dataelementselalpratwdflightsbcn
1条回答
网友
1楼 · 发布于 2024-09-29 01:27:59

检查一下这个代码。能够提取你想要的细节

from selenium import webdriver
import time

driver = webdriver.Chrome(executable_path="path")
driver.maximize_window()
driver.implicitly_wait(10)
urls = ['https://www.kayak.it/flights/MIL-BCN/2021-09-03/2021-09-04/?sort=bestflight_a','https://www.kayak.it/flights/MIL-BCN/2021-09-05/2021-09-06/?sort=bestflight_a']
all_details = dict()
for url in urls:
    driver.get(url)
    time.sleep(5)
    flights = driver.find_elements_by_class_name("resultInner")
    flights_dict = dict()
    #flights_dict = []
    i = 1
    for flight in flights:
        driver.execute_script("arguments[0].scrollIntoView(true);", flight)
        driver.execute_script("window.scrollBy(0,-300)")
        flightdetails = {}
        frowdet = []
        details = flight.find_elements_by_xpath(".//div[@class='mainInfo']//li")
        for d in details:
            fd = ""
            sd = ""
            first = d.find_elements_by_xpath(".//div[@class='top']")
            for f in first:
                fd += f.get_attribute("innerText") + ' '
            second = d.find_elements_by_xpath(".//div[@class='bottom']//span")
            for s in second:
                sd += s.get_attribute("innerText")
            detstr = sd + ' - ' + fd
            frowdet.append(detstr)

        fprice = flight.find_element_by_xpath(".//span[@class='price-text']").get_attribute("innerText")[:2]
        flightdetails["Flights"] = frowdet
        flightdetails["Price"] = fprice
        #print(flightdetails)
        flights_dict[i] = flightdetails
        i+=1
        #flights_dict.append(flightdetails)
        all_details[url] = flights_dict
for key,value in all_details.items():
    print(key,value)
driver.quit()

输出:

https://www.kayak.it/flights/MIL-BCN/2021-09-03/2021-09-04/?sort=bestflight_a {1: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 22:50 – 0:30 +1 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 19:40 – 21:10 diretto 1h 30m '], 'Price': '65'}, 2: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 22:50 – 0:30 +1 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 19:40 – 21:10 diretto 1h 30m '], 'Price': '65'}, 3: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 22:50 – 0:30 +1 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto MalpensaGOTSTN - 10:10 – 22:35 2 scali  12h 25m '], 'Price': '76'}, 4: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 14:35 – 16:15 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 19:40 – 21:10 diretto 1h 30m '], 'Price': '69'}, 5: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 21:15 – 22:55 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 19:40 – 21:10 diretto 1h 30m '], 'Price': '69'}, 6: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 10:05 – 11:35 diretto 1h 30m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 19:40 – 21:10 diretto 1h 30m '], 'Price': '73'}, 7: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 21:15 – 22:55 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 17:50 – 19:35 diretto 1h 45m '], 'Price': '76'}, 8: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 22:50 – 0:30 +1 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 17:50 – 19:35 diretto 1h 45m '], 'Price': '76'}, 9: {'Flights': ['LIN Aeroporto Milano Linate‐BCN Barcellona-El Prat - 16:35 – 18:05 diretto 1h 30m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 19:40 – 21:10 diretto 1h 30m '], 'Price': '79'}, 10: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 17:20 – 19:00 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 19:40 – 21:10 diretto 1h 30m '], 'Price': '79'}, 11: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 22:50 – 0:30 +1 diretto 1h 40m ', 'BCN Barcellona-El Prat‐BGY Bergamo Orio al Serio - 20:10 – 21:50 diretto 1h 40m '], 'Price': '80'}, 12: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 14:35 – 16:15 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 17:50 – 19:35 diretto 1h 45m '], 'Price': '80'}, 13: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 21:15 – 22:55 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 7:35 – 9:20 diretto 1h 45m '], 'Price': '81'}, 14: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 22:50 – 0:30 +1 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 7:35 – 9:20 diretto 1h 45m '], 'Price': '81'}, 15: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 10:05 – 11:35 diretto 1h 30m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 17:50 – 19:35 diretto 1h 45m '], 'Price': '84'}, 16: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 21:15 – 22:55 diretto 1h 40m ', 'BCN Barcellona-El Prat‐BGY Bergamo Orio al Serio - 20:10 – 21:50 diretto 1h 40m '], 'Price': '84'}, 17: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 14:35 – 16:15 diretto 1h 40m ', 'BCN Barcellona-El Prat‐BGY Bergamo Orio al Serio - 20:10 – 21:50 diretto 1h 40m '], 'Price': '84'}}
https://www.kayak.it/flights/MIL-BCN/2021-09-05/2021-09-06/?sort=bestflight_a {1: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:25 – 10:00 diretto 1h 35m ', 'BCN Barcellona-El Prat‐LIN Aeroporto Milano Linate - 19:05 – 20:45 diretto 1h 40m '], 'Price': '38'}, 2: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:25 – 10:00 diretto 1h 35m ', 'BCN Barcellona-El Prat‐LIN Aeroporto Milano Linate - 19:05 – 20:45 diretto 1h 40m '], 'Price': '38'}, 3: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:10 – 9:45 diretto 1h 35m ', 'BCN Barcellona-El Prat‐LIN Aeroporto Milano Linate - 19:05 – 20:45 diretto 1h 40m '], 'Price': '46'}, 4: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:25 – 10:00 diretto 1h 35m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 14:50 – 16:35 diretto 1h 45m '], 'Price': '49'}, 5: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:10 – 9:45 diretto 1h 35m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 14:50 – 16:35 diretto 1h 45m '], 'Price': '57'}, 6: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 9:55 – 11:35 diretto 1h 40m ', 'BCN Barcellona-El Prat‐LIN Aeroporto Milano Linate - 19:05 – 20:45 diretto 1h 40m '], 'Price': '58'}, 7: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:25 – 10:00 diretto 1h 35m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 14:20 – 16:00 diretto 1h 40m '], 'Price': '63'}, 8: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:25 – 10:00 diretto 1h 35m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 18:55 – 20:40 diretto 1h 45m '], 'Price': '68'}, 9: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:25 – 10:00 diretto 1h 35m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 12:15 – 14:00 diretto 1h 45m '], 'Price': '68'}, 10: {'Flights': ['MXP Aeroporto Malpensa‐BCN Barcellona-El Prat - 9:55 – 11:35 diretto 1h 40m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 14:50 – 16:35 diretto 1h 45m '], 'Price': '69'}, 11: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:25 – 10:00 diretto 1h 35m ', 'BCN Barcellona-El Prat‐BGY Bergamo Orio al Serio - 6:20 – 8:00 diretto 1h 40m '], 'Price': '78'}, 12: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:10 – 9:45 diretto 1h 35m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 14:20 – 16:00 diretto 1h 40m '], 'Price': '71'}, 13: {'Flights': ['LIN Aeroporto Milano Linate‐BCN Barcellona-El Prat - 16:35 – 18:05 diretto 1h 30m ', 'BCN Barcellona-El Prat‐LIN Aeroporto Milano Linate - 19:05 – 20:45 diretto 1h 40m '], 'Price': '72'}, 14: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:10 – 9:45 diretto 1h 35m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 18:55 – 20:40 diretto 1h 45m '], 'Price': '76'}, 15: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:25 – 10:00 diretto 1h 35m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 7:45 – 9:30 diretto 1h 45m '], 'Price': '74'}, 16: {'Flights': ['BGY Bergamo Orio al Serio‐BCN Barcellona-El Prat - 8:10 – 9:45 diretto 1h 35m ', 'BCN Barcellona-El Prat‐MXP Aeroporto Malpensa - 12:15 – 14:00 diretto 1h 45m '], 'Price': '76'}}

相关问题 更多 >