简体   繁体   中英

Read individual parts from HTML .txt file

What I want to achieve?

  • I want to download an HTML text file with data of different players .
  • All data has one specific format.
  • I want to read specific data about one player and store it.

This is the data format I am reading (it is all separated by : ):

callsign:cid:realname:clienttype:frequency:latitude:longitude:altitude:groundspeed:planned_aircraft:planned_tascruise:planned_depairport:planned_altitude:planned_destairport:server:protrevision:rating:transponder:facilitytype:visualrange:planned_revision:planned_flighttype:planned_deptime:planned_actdeptime:planned_hrsenroute:planned_minenroute:planned_hrsfuel:planned_minfuel:planned_altairport:planned_remarks:planned_route:planned_depairport_lat:planned_depairport_lon:planned_destairport_lat:planned_destairport_lon:atis_message:time_last_atis_received:time_logon:heading:QNH_iHg:QNH_Mb:

That's how I read the HTML file:

import requests

link = "data.vatsim.net/vatsim-data.txt"
f = requests.get(link) 
print(f.text)

Okey hi so this is the working code with explanation:

import requests

#Get the data
link = "http://data.vatsim.net/vatsim-data.txt"
f = requests.get(link) 

players = str(f.text).split("!CLIENTS:")[1]

#Find what attributes we want to search for and store their idex inside 'attributes_to_find' dictionary as values
#ETA is not in the data! Couldn't find it.
attributes = "callsign:cid:realname:clienttype:frequency:latitude:longitude:altitude:groundspeed:planned_aircraft:planned_tascruise:planned_depairport:planned_altitude:planned_destairport:server:protrevision:rating:transponder:facilitytype:visualrange:planned_revision:planned_flighttype:planned_deptime:planned_actdeptime:planned_hrsenroute:planned_minenroute:planned_hrsfuel:planned_minfuel:planned_altairport:planned_remarks:planned_route:planned_depairport_lat:planned_depairport_lon:planned_destairport_lat:planned_destairport_lon:atis_message:time_last_atis_received:time_logon:heading:QNH_iHg:QNH_Mb:"
attributes = attributes.split(":")

attributes_to_find = {"callsign":0, "planned_route":0, "altitude":0}

for i, attribute in enumerate(attributes):
    if attribute in attributes_to_find:
        attributes_to_find[attribute] = i

#Function to find the player
def findPlayer(players, name = "None"): 
    players = players.split('\n')

    for player in players: #Search all players
        if name in player: #If name is in player data
            for key, value in attributes_to_find.items(): #Search all attributes that we are looking for and print them
                print("{}: '{}'".format(key, player.split(":")[value]))

            return #If we found it return

findPlayer(players, "Carlos Marques LPPR")

This is the output of the code above:

callsign: 'UAE140'
planned_route: 'ANPUB1B ANPUB P627 GUTOX/N0495F340 P627 KADAP R348 LUXAG/N0485F360 R348 RUPIG UR348 TNV UB536 EROPA UT446 WIV WIV4A'
altitude: '36109'

I couldn't find the ETA attribute. But if you want to read any other attribute just add it to the attributes_to_find = {"callsign":0, "planned_route":0, "altitude":0} dictionary.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM