-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcarriers.py
executable file
·69 lines (55 loc) · 2.05 KB
/
carriers.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
#!/home/sgrosu/anaconda2/bin/python
# -*- coding: utf-8 -*-
"""
Your task in this exercise is to modify 'extract_carrier()` to get a list of
all airlines. Exclude all of the combination values like "All U.S. Carriers"
from the data that you return. You should return a list of codes for the
carriers.
All your changes should be in the 'extract_carrier()' function. The
'options.html' file in the tab above is a stripped down version of what is
actually on the website, but should provide an example of what you should get
from the full file.
Please note that the function 'make_request()' is provided for your reference
only. You will not be able to to actually use it from within the Udacity web UI.
"""
from bs4 import BeautifulSoup
html_page = "options.html"
def extract_carriers(page):
data = []
with open(page, "r") as html:
# do something here to find the necessary values
soup = BeautifulSoup(html, "lxml")
# for carriers use id=CarrierList
select = soup.find(id="AirportList")
options = select.find_all("option")
#clist = []
for el in options:
if "All" not in el.get("value"):
data.append(el.get("value"))
#print clist
return data
'''
def make_request(data):
eventvalidation = data["eventvalidation"]
viewstate = data["viewstate"]
airport = data["airport"]
carrier = data["carrier"]
r = s.post("https://www.transtats.bts.gov/Data_Elements.aspx?Data=2",
data = (("__EVENTTARGET", ""),
("__EVENTARGUMENT", ""),
("__VIEWSTATE", viewstate),
("__VIEWSTATEGENERATOR",viewstategenerator),
("__EVENTVALIDATION", eventvalidation),
("CarrierList", carrier),
("AirportList", airport),
("Submit", "Submit")))
return r.text
def test():
data = extract_carriers(html_page)
assert len(data) == 16
assert "FL" in data
assert "NK" in data
if __name__ == "__main__":
test()
'''
print extract_carriers(html_page)