@@ -0,0 +1,2333 @@
+#!/usr/bin/env python%0A# -*- coding: utf-8 -*-%0A%0A%22%22%22%0AScript to get information from:%0Ahttp://www.transtats.bts.gov/Data_Elements.aspx?Data=2%0Aabout carrier and airports%0A%22%22%22%0A%0Afrom bs4 import BeautifulSoup%0Aimport requests%0Aimport urllib2%0A%0A%0A%0Adef extract_data(url, s):%0A    # Extract data from a html source from a URL%0A    r = s.get(url)%0A    soup = BeautifulSoup(r.text)%0A    data = %7B%22eventvalidation%22: %22%22,%0A            %22viewstate%22: %22%22%7D%0A%0A    eventvalidation_element = soup.find(id=%22__EVENTVALIDATION%22)%0A    data%5B%22eventvalidation%22%5D = eventvalidation_element%5B%22value%22%5D%0A%0A    viewstate_element = soup.find(id=%22__VIEWSTATE%22)%0A    data%5B%22viewstate%22%5D = viewstate_element%5B%22value%22%5D%0A%0A    return data%0A%0A%0A%0A%0Adef make_request(data, s):%0A    # Make request to get data%0A    eventvalidation = data%5B%22eventvalidation%22%5D%0A    viewstate = data%5B%22viewstate%22%5D%0A%0A    r = s.post(%22http://www.transtats.bts.gov/Data_Elements.aspx?Data=2%22,%0A               data=%7B'AirportList': %22BOS%22,%0A                     'CarrierList': %22VX%22,%0A                     'Submit': 'Submit',%0A                     %22__EVENTTARGET%22: %22%22,%0A                     %22__EVENTARGUMENT%22: %22%22,%0A                     %22__EVENTVALIDATION%22: eventvalidation,%0A                     %22__VIEWSTATE%22: viewstate%0A                     %7D)%0A%0A    return r.text%0A%0A%0Adef make_file(html):%0A    # Make file with the result data%0A    f = open(%22text.html%22, %22w%22)%0A    f.write(html)%0A%0A%0Adef options(soup, id):%0A    # Get data about options: airport and carriers%0A    option_values = %5B%5D%0A    carrier_list = soup.find(id=id)%0A    for option in carrier_list.find_all('option'):%0A        option_values.append(option%5B'value'%5D)%0A    return option_values%0A%0A%0Adef print_list(label, codes):%0A    # Print data%0A    print %22%5Cn%25s:%22 %25 label%0A    for c in codes:%0A        print c%0A%0A%0Adef get_web(url):%0A    # Get url%0A    page = urllib2.urlopen(url)%0A    page_source = page.read()%0A    return page_source%0A%0Adef main():%0A    # setup the location files%0A    URL = 'http://www.transtats.bts.gov/Data_Elements.aspx?Data=2'%0A%0A    page_source = get_web(URL)%0A    soup = BeautifulSoup(page_source)%0A%0A    codes = options(soup, %22CarrierList%22)%0A    print_list(%22Carriers%22, codes)%0A%0A    codes = options(soup, %22AirportList%22)%0A    print_list(%22Airports%22, codes)%0A%0A    s = requests.Session()%0A    data = extract_data(URL, s)%0A%0A    html = make_request(data, s)%0A%0A    make_file(html)%0A%0Aif __name__ == '__main__':%0A    main()%0A

Create file to get information from a particular URL to get data about airplanes and carriers.