Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import requests
- import csv
- # Configuración
- api_key = 'DKJNJbahjvhxavscjjyu7388' # ¡Reemplaza con tu API key real!
- aeropuertos_mexico = ['MEX', 'MTY', 'GDL', 'CUN', 'TIJ', 'HMO'] # Principales aeropuertos
- # Archivo CSV
- with open('vuelos_mexico.csv', 'w', newline='', encoding='utf-8') as file:
- writer = csv.writer(file)
- writer.writerow(['Vuelo', 'Aerolínea', 'Aeropuerto_Salida', 'Destino', 'Estado', 'Hora_Salida'])
- # Consultar cada aeropuerto
- for aeropuerto in aeropuertos_mexico:
- url = f"http://api.aviationstack.com/v1/flights?access_key={api_key}&dep_iata={aeropuerto}"
- response = requests.get(url)
- data = response.json()
- print(f"\n✈️ Vuelos desde {aeropuerto}:\n")
- if 'data' in data and data['data']:
- for flight in data['data']:
- # Datos para CSV
- vuelo = flight['flight']['iata']
- aerolinea = flight['airline']['name']
- destino = flight['arrival']['airport']
- estado = flight['flight_status']
- hora_salida = flight['departure']['scheduled'].split('T')[1][:5] if flight['departure']['scheduled'] else "N/A"
- # Escribir en CSV
- writer.writerow([vuelo, aerolinea, aeropuerto, destino, estado, hora_salida])
- # Imprimir en consola
- print(f"🔹 {vuelo} | {aerolinea}")
- print(f" Destino: {destino}")
- print(f" Hora: {hora_salida} | Estado: {estado.upper()}")
- print("-" * 50)
- else:
- print(f"No hay vuelos registrados en {aeropuerto}.")
- print("\n✅ Datos guardados en 'vuelos_mexico.csv'")
Advertisement
Add Comment
Please, Sign In to add comment