How to scrape real time streaming data with Python?

后端 未结 3 620
故里飘歌
故里飘歌 2021-01-31 12:59

I was trying to scrape the number of flights for this webpage https://www.flightradar24.com/56.16,-49.51

The number is highlighted in the picture below:

The num

3条回答
  •  [愿得一人]
    2021-01-31 13:18

    So based on what @Andre has found out, I wrote this code:

    import requests
    from bs4 import BeautifulSoup
    import time
    
    def get_count():
        url = "https://data-live.flightradar24.com/zones/fcgi/feed.js?bounds=59.09,52.64,-58.77,-47.71&faa=1&mlat=1&flarm=1&adsb=1&gnd=1&air=1&vehicles=1&estimated=1&maxage=7200&gliders=1&stats=1"
    
        # Request with fake header, otherwise you will get an 403 HTTP error
        r = requests.get(url, headers={'User-Agent': 'Mozilla/5.0'})
    
        # Parse the JSON
        data = r.json()
        counter = 0
    
        # Iterate over the elements to get the number of total flights
        for element in data["stats"]["total"]:
            counter += data["stats"]["total"][element]
    
        return counter
    
    while True:
        print(get_count())
        time.sleep(8)
    

    The code should be self explaining, everything it does is printing the actual flight count every 8 seconds :)

    Note: The values are similar to the ones on the website, but not the same. This is because it's unlikely, that the Python script and the website are sending a request at the same time. If you want to get more accurate results, just make a request every 4 seconds for example.

    Use this code as you want, extend it or whatever. Hope this helps!

提交回复
热议问题