Created
February 22, 2017 06:49
-
-
Save testflyjets/97e11f2c20c7234fac8d94b54725e199 to your computer and use it in GitHub Desktop.
AWS Lambda function to scrape FAA N-number data
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| from __future__ import print_function | |
| import boto3 | |
| import json | |
| import urllib2 | |
| from bs4 import BeautifulSoup | |
| print('Loading function') | |
| def handler(event, context): | |
| #print("Received event: " + json.dumps(event, indent=2)) | |
| nnumber = event['nnumber'] | |
| return query_nnumber(nnumber) | |
| def query_nnumber(nnumber): | |
| #specify the url | |
| n_number_inquiry = 'http://registry.faa.gov/aircraftinquiry/NNum_Results.aspx?NNumbertxt=%s' % nnumber | |
| #Query the website and return the html to the variable 'page' | |
| page = urllib2.urlopen(n_number_inquiry) | |
| data_dict = { | |
| 'serial_no': 'content_lbSerialNo', | |
| 'mfr_name': 'content_lbMfrName', | |
| 'ac_model': 'content_Label7', | |
| 'ac_type': 'content_Label11', | |
| 'engine_type': 'content_lbTypeEng', | |
| 'reg_type': 'content_lbTypeReg', | |
| 'owner_name': 'content_lbOwnerName', | |
| 'owner_address': 'content_lbOwnerStreet', | |
| 'owner_city': 'content_lbOwnerCity', | |
| 'owner_state': 'content_lbOwnerState', | |
| 'owner_zip': 'content_lbOwnerZip', | |
| 'owner_county': 'content_lbOwnerCounty', | |
| 'owner_country': 'content_lbOwnerCountry' | |
| } | |
| soup = BeautifulSoup(page, 'html.parser') | |
| # data_spans = soup('span', class_='Results_DataText') | |
| # | |
| # print "Found %d data elements" % len(data_spans) | |
| # | |
| # for span in data_spans: | |
| # print span.contents[0].strip() | |
| data = {} | |
| for fld, span_id in data_dict.iteritems(): | |
| data[fld] = soup.find('span', class_='Results_DataText', id=span_id).contents[0].strip() | |
| return json.dumps(data) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment