Skip to content

Instantly share code, notes, and snippets.

@echang26
Last active March 23, 2016 22:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save echang26/51ecb760e32473b1d920 to your computer and use it in GitHub Desktop.
Save echang26/51ecb760e32473b1d920 to your computer and use it in GitHub Desktop.
import urllib2
import json
import csv
def get_info(api):
try:
data = urllib2.urlopen(api)
response_data = json.load(data)
data.close()
except urllib2.HTTPError, e:
print "HTTP error: {}".format(e.code)
except urllib2.URLError, e:
print "Network error: {}".format(e.reason.args[1])
item_keys = []
write_header = True
with open('nyc_jobs2.csv', 'wb') as csv_file:
writer = csv.writer(csv_file)
for item in response_data:
item_values = []
for key in item:
if write_header:
item_keys.append(key)
value = item.get(key, '')
#check if the vlue is a string, and if so, convert to unicode
if isinstance(value, basestring):
item_values.append(value.encode('utf-8'))
else:
item_values.append(value)
if write_header:
writer.writerow(item_keys)
write_header = False
writer.writerow(item_values)
csv_file.close()
api_link = "https://data.cityofnewyork.us/resource/kpav-sd4t.json"
get_info(api_link)
#outputs a csv file with the following fields:
#minimum_qual_requirements,salary_range_from,additional_information,job_description,to_apply,title_code_no,process_date,civil_service_title,posting_updated,job_id,agency,division_work_unit,business_title,__of_positions,hours_shift,work_location_1,residency_requirement,salary_range_to,posting_type,work_location,preferred_skills,salary_frequency,level,posting_date
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment