Skip to content

Instantly share code, notes, and snippets.

@aambrioso1
Created January 3, 2020 09:26
Show Gist options
  • Save aambrioso1/167c11da05d0d7711ea91aa4cd8f53a9 to your computer and use it in GitHub Desktop.
Save aambrioso1/167c11da05d0d7711ea91aa4cd8f53a9 to your computer and use it in GitHub Desktop.
noaa_json.py
import json, requests, sys
"""
Example of using the request package for json data from NOAA.
Here are Links to the documentation for these important libraries and the home of JSON.
https://docs.python.org/3/library/json.html
https://docs.python.org/3/library/sys.html
https://pypi.org/project/requests/
http://json.org/
Website with NOWA API information:
https://tidesandcurrents.noaa.gov/api/
"""
# Typical URL for the request.
# url = 'https://tidesandcurrents.noaa.gov/api/datagetter?begin_date=20130808 15:00&end_date=20130808 15:06&station=8454000&product=water_temperature&units=english&time_zone=gmt&application=ports_screen&format=json'
start = '20130808 15:00'
end = '20130808 15:06'
start2 = '20200103 00:00'
end2 = '20200103 05:00'
station1 = '8454000'
station2 = '8726537'
station3 = '8726384'
zone1 = 'gmt'
zone2 = 'lst'
url = 'https://tidesandcurrents.noaa.gov/api/datagetter?begin_date={}&end_date={}&station={}&product=water_temperature&units=english&time_zone={}&application=ports_screen&format=json'.format(start2, end2, station3, zone2)
response = requests.get(url)
response.raise_for_status()
# Load JSON data into a Python variable.
data = json.loads(response.text)
print(data['metadata']['name'])
# Print weather descriptions.
d = data['data']
print('Station data:\n', d)
print('*' * 35)
for item in d:
print('Time: {}: temp: {}'.format(item['t'], item['v']))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment