简体   繁体   English

从 HTML.txt 文件中读取各个部分

[英]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 .我想下载一个包含不同players器数据的 HTML 文本文件。
  • 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:这就是我阅读 HTML 文件的方式:

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:这是上面代码的output:

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.我找不到 ETA 属性。 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.但是,如果您想读取任何其他属性,只需将其添加到attributes_to_find = {"callsign":0, "planned_route":0, "altitude":0}字典。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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