Issue
I am trying to get a list of all the airports from https://www.flight-distance.com/ Select Origin dropdown. I have managed to get the start of the data with the below code. but I can't figure out how to get all the options from the site as shown in the attached image.(Only starting to learn Python)
So for eg. the list would start with Goroka (GKA, AYGA), Madang (MAG, AYMD), Mount Hagen (HGU, AYMH), etc.
import requests
from bs4 import BeautifulSoup
r=requests.get("http://www.flight-distance.com/")
#type(r)
c=r.content
soup=BeautifulSoup(c,"html.parser")
all=soup.find_all("select",{"id":"source_airport"})
all
Result:
> [<select class="form-control single_sel_air" id="source_airport"
> name="source_airport"> <option value="">Please select airport or
> city</option> </select>]
Really appreciate any and all help
Solution
when the page loads, the list of airports is loaded dynamically, you can do it like this:
url = 'https://www.flight-distance.com/get_airports'
response = requests.get(url)
for airport in response.json()['data']:
print(airport['Name'], airport['IATA_FAA'], airport['ICAO'])
OUTPUT:
Goroka GKA AYGA
Madang MAG AYMD
Mount Hagen HGU AYMH
Nadzab LAE AYNZ
Port Moresby Jacksons Intl POM AYPY
Wewak Intl WWK AYWK
Narsarsuaq UAK BGBW
Nuuk GOH BGGH
Sondre Stromfjord SFJ BGSF
Thule Air Base THU BGTL
Akureyri AEY BIAR
Egilsstadir EGS BIEG
Hornafjordur HFN BIHN
Husavik HZK BIHU
Isafjordur IFJ BIIS
Keflavik Nas KEF BIKF
Patreksfjordur PFJ BIPA
Reykjavik RKV BIRK
Siglufjordur SIJ BISI
...........
Answered By - Sergey K
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.