-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrbitalLaunches.py
70 lines (53 loc) · 1.88 KB
/
OrbitalLaunches.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import requests
from datetime import datetime, timezone, timedelta
from bs4 import BeautifulSoup
import pytz
# function to check if any of the payloads are one of status Operational, Successful or En Route
def isOneOfTheCategories(list):
validOutcome = ['Operational','Successful','En Route']
for l in list:
tds = l.find_all("td")
name = tds[-1].text.strip()
if(name.startswith('Operational') or name.startswith('Successful') or name.startswith('En Route')):
return True
return False
# get webpage
result = requests.get("https://en.wikipedia.org/wiki/2019_in_spaceflight#Orbital_launches")
src = result.content
soup = BeautifulSoup(src,'lxml')
tables = soup.find_all("table")
Orbital_launches = tables[3]
trs = Orbital_launches.find_all("tr")
data = {}
i = 0
offset = 0
# recoring data locally to count number of launches getting from each table row feom webpage
while i < len(trs):
if( trs[i].find("td") is not None and (trs[i].find("td").has_attr("rowspan"))):
if(trs[i].find("td").has_attr("rowspan")):
offset = int(trs[i].find("td")['rowspan'])
if(isOneOfTheCategories(trs[i+1:i+offset])):
mydate = datetime.strptime(trs[i].find("td").find("span").text.strip()+" 2019", "%d %B %Y")
if(str(mydate) in data):
data[str(mydate)] = data[str(mydate)] + 1
else:
data[str(mydate)] = 1
i = i + offset
else:
offset = 0
i = i + 1
start_date = datetime(year=2019, month=1, day=1)
end_date = datetime(year=2019, month=12, day=31)
delta = timedelta(days=1)
timezone = pytz.timezone('UTC')
f = open("output.csv", "w")
# looping through the all dates in 2019 and writing to csv file
while start_date <= end_date:
if(str(start_date) in data):
f.write(str(timezone.localize(start_date).isoformat())+", "+str(data[str(start_date)]))
f.write("\n")
else:
f.write(str(timezone.localize(start_date).isoformat())+", 0")
f.write("\n")
start_date += delta
f.close()