-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathcarriers.py
More file actions
35 lines (29 loc) · 975 Bytes
/
Copy pathcarriers.py
File metadata and controls
35 lines (29 loc) · 975 Bytes
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
"""
Return a list of codes for all airline carriers.
Excludes all of the combination values like "All U.S. Carriers"
"""
## Import BeautifulSoup
from bs4 import BeautifulSoup
import csv
## Function
def extract_carriers(page):
### Get a list of Carriers
carrier_values = []
with open(page, "r") as html:
#### Setup BeautifulSoup
soup = BeautifulSoup(html, "lxml")
#### Create list of Carriers
carrier_list = soup.find(id="CarrierList")
carriers = carrier_list.find_all('option')
for carrier in carriers:
carrier_values.append(carrier['value'])
return carrier_values[3:]
## Output Carriers
carriers = extract_carriers("airport_and_carrier.html")
print("Number of Carriers:")
print(len(carriers)) # 16
with open('carriers.csv', 'w', newline='') as csvfile:
writer = csv.writer(csvfile, delimiter=' ', quotechar='|', quoting=csv.QUOTE_MINIMAL)
writer.writerow("Carriers")
for carrier in carriers:
writer.writerow(carrier)