Mar-25-2021, 04:41 PM
First of all - sorry to tell you, but you are doing it wrong.
Also, if you are going to scrap large amount of information - maybe it's better to look at some database, e.g. sqlite3 support comes with python standard library. Also consider that API can blok you if you are heavy on them - check their allowed limits.
Finally - maybe you need to take some tutorial/course, incl. about objects, OOP, etc.
get_flight()
returns a list of FlightRadar24.flight.Flight
objects. Every object has methods and you can access them, no need for all this complication to convert to string and then parse it.import csv from datetime import datetime from FlightRadar24.api import FlightRadar24API fr_api = FlightRadar24API() flights = fr_api.get_flights(airline = 'AZU') print(type(flights[0])) print(flights[0].__dict__) with open('fr24.csv', 'w') as f: fieldnames = ['time', 'aircraft_code', 'registration', 'altitude', 'ground_speed', 'heading'] # you can include whatever attributes you want wrtr = csv.DictWriter(f, fieldnames=fieldnames, extrasaction='ignore') wrtr.writeheader() for flight in flights: flight.time = datetime.fromtimestamp(flight.time) # convert the timestamp to datetime object wrtr.writerow(flight.__dict__) # here I pass all attributes as dict, but you can always access individual attributes, like I did with time abovethe output
Output:<class 'FlightRadar24.flight.Flight'>
{'id': '273002c6', 'icao_24bit': 'E495B3', 'latitude': -23.6238, 'longitude': -46.6602, 'heading': 236, 'altitude': 0, 'ground_speed': 4, 'squawk': '5107', 'aircraft_code': 'A20N', 'registration': 'PR-YSE', 'time': 1616685581, 'origin_aiport_iata': 'REC', 'destination_airport_iata': 'CGH', 'number': 'AD4251', 'airline_iata': 'AD', 'on_ground': 1, 'vertical_speed': 0, 'callsign': 'AZU4251', 'airline_icao': 'AZU'}
fr24.csvOutput:time,aircraft_code,registration,altitude,ground_speed,heading
2021-03-25 17:19:41,A20N,PR-YSE,0,4,236
2021-03-25 17:19:42,A20N,PR-YSD,3500,141,73
2021-03-25 17:19:37,E195,PR-AUK,36000,448,336
2021-03-25 17:19:40,A20N,PR-YRN,0,12,132
2021-03-25 17:19:42,A20N,PR-YRD,37050,470,27
2021-03-25 17:19:41,A339,PR-ANX,40000,462,332
2021-03-25 17:19:41,AT76,PR-AQQ,16525,301,350
2021-03-25 17:19:42,A20N,PR-YYG,37825,459,359
2021-03-25 17:19:38,AT76,PR-AQJ,8075,197,350
2021-03-25 17:19:42,E195,PR-AYO,6875,198,60
2021-03-25 17:19:41,C208,PT-MED,4700,173,108
2021-03-25 17:19:42,AT76,PR-AQR,21025,267,92
2021-03-25 17:19:41,E295,PS-AEG,4625,172,73
2021-03-25 17:19:38,AT76,PR-AKB,10075,256,269
2021-03-25 17:19:42,E195,PR-AUJ,3350,153,128
2021-03-25 17:19:42,E295,PR-PJN,4525,193,71
2021-03-25 17:19:42,E195,PR-AUH,37000,474,8
2021-03-25 17:19:39,A20N,PR-YRE,0,19,309
2021-03-25 17:19:42,A20N,PR-YSG,38000,441,221
2021-03-25 17:19:41,A20N,PR-YRW,12000,259,307
2021-03-25 17:19:42,AT76,PR-AKF,16925,179,347
2021-03-25 17:19:41,A21N,PR-YJB,27800,478,313
2021-03-25 17:19:41,A20N,PR-YYJ,0,12,317
Now, I didn't look into details - e.g. iz it possible to retrieve info for multiple companies or you need to make separate requests, but you get the idea.Also, if you are going to scrap large amount of information - maybe it's better to look at some database, e.g. sqlite3 support comes with python standard library. Also consider that API can blok you if you are heavy on them - check their allowed limits.
Finally - maybe you need to take some tutorial/course, incl. about objects, OOP, etc.