Last active
February 14, 2021 14:41
-
-
Save 3ng1n33r/8b9f539d1371e6699b734aeaf982e92e to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
import re | |
import fileinput | |
import time | |
from bs4 import BeautifulSoup | |
from datetime import datetime | |
def timestamp(): | |
now = datetime.now() | |
ts = now.strftime("%Y-%m-%d %H:%M:%S") | |
return ts | |
def get_html(url): | |
headers = { | |
'Accept-Encoding': 'gzip, deflate, sdch', | |
'Accept-Language': 'en-US,en;q=0.8', | |
'Upgrade-Insecure-Requests': '1', | |
'User-Agent': 'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/56.0.2924.87 Safari/537.36', | |
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', | |
'Cache-Control': 'max-age=0', | |
'Connection': 'keep-alive', | |
} | |
try: | |
response = requests.get(url,headers=headers,timeout=10) | |
if response.status_code == 429: | |
time.sleep(int(response.headers["Retry-After"])) | |
response = requests.get(url,headers=headers,timeout=10) | |
except requests.exceptions.RequestException as err: | |
raise SystemExit(err) | |
return response.content | |
def parse(html): | |
soup = BeautifulSoup(html, 'html.parser') | |
table = soup.find_all('span', {"class": "details"}) | |
if table: | |
icao = re.sub("^\s+|\s+$", "", table[6].text, flags=re.UNICODE) | |
typeac = re.sub("^\s+|\s+$", "", table[3].text, flags=re.UNICODE) | |
aircraft = re.sub("^\s+|\s+$", "", table[0].text, flags=re.UNICODE) | |
airline = re.sub("^\s+|\s+$", "", table[1].text, flags=re.UNICODE) | |
oper = re.sub("^\s+|\s+$", "", table[2].text, flags=re.UNICODE) | |
print ("%s,%s,%s,%s,%s,%s,%s" %(icao, reg, typeac, aircraft, airline, oper, timestamp())) | |
time.sleep(2) | |
else: | |
print ("-,%s,-,-,-,-,%s" %(reg, timestamp())) | |
time.sleep(2) | |
def main(): | |
short_url = 'https://www.flightradar24.com/data/aircraft/' | |
global reg | |
#reg = 'RA-85625' | |
for line in fileinput.input(): | |
reg = line.rstrip() | |
time.sleep(1) | |
parse(get_html(short_url+reg)) | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment