Created
November 8, 2017 03:58
-
-
Save Evantm/f03014a2c67e2ea745d69c1175f8f8ed to your computer and use it in GitHub Desktop.
Script to parse Purple Air given search term
This file contains 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
import requests | |
import urllib | |
import json | |
url = """ https://www.purpleair.com/json?&sensorsActive=168&orderby=L """ | |
Search_Term = "Kamloops" | |
req = urllib.request.Request( | |
url=url, | |
data=None, | |
headers={ | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.47 Safari/537.36' | |
} | |
) | |
f = urllib.request.urlopen(req).read().decode('windows-1252') | |
big_json = json.loads(f) | |
for n in big_json['results']: | |
#330 is TRU | |
#387 is monmouth | |
if(n['Label'].find(Search_Term) > -1): | |
#print(n) | |
name = n['Label'] | |
print(name) | |
#(°F - 32) x 5/9 = °C | |
c = (float(n['temp_f']) - 32) * 5/9 | |
print('Temp','%.2f °C' % c) | |
h = n['humidity'] | |
print('Humidity','%.2f %%' % float(h)) | |
p = n['pressure'] | |
print('Pressure','%.2f kpa' % float(p)) | |
pm2 = n['PM2_5Value'] | |
print('PM2_5 Value','%.2f µg/m3' % float(pm2)) | |
print() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment