-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAirflight stats - project 22oct18.py
157 lines (100 loc) · 3.55 KB
/
Airflight stats - project 22oct18.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
#!/usr/bin/env python
# coding: utf-8
# In[1]:
from bs4 import BeautifulSoup
import requests
import json
from pathlib import Path
# In[2]:
html_page = "downloaded_page_for_list_extraction.html"
# In[3]:
# extract carrier list values
def extract_data(page):
data = []
with open(page, "r") as html:
soup = BeautifulSoup(html, "lxml")
carr = soup.find(id = "CarrierList")
for rows in carr.find_all('option'):
val = (rows['value'])
if(len(val) == 2): #Not selecting carrier grouped values like 'all'
data.append(val)
return data
# extract airport list values
def extract_airports(page):
data = []
with open(page, "r") as html:
soup = BeautifulSoup(html, "lxml")
select = soup.find(id = "AirportList")
for row in select.find_all("option"):
val = row['value']
if(val[0:3].lower() != 'all'): #Not selecting airport grouped values like 'all'
data.append(val)
return data
# In[4]:
carrier_list = extract_data(html_page)
airport_list = extract_airports(html_page)
# In[5]:
# Total number of carrier-airport combinations
print(len(airport_list) * len(carrier_list))
# In[ ]:
URL = "https://www.transtats.bts.gov/Data_Elements.aspx?Data=2"
s = requests.Session()
resp = s.get(URL)
soup = BeautifulSoup(resp.text, "lxml")
#Finding header values for the requests
viewstate = soup.find(id = "__VIEWSTATE")['value']
eventvalidation = soup.find(id = "__EVENTVALIDATION")['value']
viewstategenerator = soup.find(id = "__VIEWSTATEGENERATOR")['value']
# In[10]:
def new_session():
URL = "https://www.transtats.bts.gov/Data_Elements.aspx?Data=2"
s = requests.Session()
resp = s.get(URL)
soup = BeautifulSoup(resp.text, "lxml")
viewstate = soup.find(id = "__VIEWSTATE")['value']
eventvalidation = soup.find(id = "__EVENTVALIDATION")['value']
viewstategenerator = soup.find(id = "__VIEWSTATEGENERATOR")['value']
print(viewstate)
# In[17]:
#Keeping count of carrier-airport combinations with no data
exists_counter = 0
does_not_exist_counter = 0
#Sending requests and HTML response into file
for carrier in carrier_list:
for airport in airport_list:
body = {
"__EVENTTARGET" : "",
"__EVENTARGUMENT" : "",
"__VIEWSTATE" : viewstate,
"__VIEWSTATEGENERATOR" : viewstategenerator,
"CarrierList" : carrier,
"AirportList" : airport,
"Submit" : "submit",
"__EVENTVALIDATION" : eventvalidation
}
resp = s.post(URL, data = body)
f = open("./flight_data/"+carrier+"_"+airport+".html", "w")
(f.write(resp.text))
f.close()
output_file_name = "./flight_data/"+carrier+"_"+airport+".html"
html_response_file = Path(output_file_name)
if html_response_file.is_file():
exists_counter +=1
pass
else:
does_not_exist_counter +=1
while True:
try:
resp = s.post(URL, data = body)
except Exception as e:
print(e)
print('renewing session', '\n') #If session times out. Renewing it
new_session()
continue
break
f = open(output_file_name, "w")
(f.write(resp.text))
f.close()
# In[13]:
print(exists_counter)
print(does_not_exist_counter)