Last active
February 6, 2017 20:07
-
-
Save mbaltrusitis/c1c796ebb3c03c7d7239e8e32228f206 to your computer and use it in GitHub Desktop.
A quick and dirty URL request and JSON decoding/deserialization.
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
import json | |
from urllib import request | |
from pprint import pprint | |
def main(): | |
weather_req = request.urlopen("http://api.wunderground.com/api/872a930f94a85692/conditions/q/UK/London.json") | |
# read the request and decode the bytes into sensible UTF-8 strings | |
weather_data = weather_req.read().decode("utf-8") | |
# take a peak at the mess of it | |
pprint(weather_data) | |
# lets turn it into something useful by decoding it: | |
weather_data = json.loads(weather_data) | |
# lets just save the key we care about, "current_observation" | |
weather_data = weather_data["current_observation"] | |
pprint(weather_data) | |
# this "useful" thing is called a `dict` in Python. We can access things by its attributes now like this: | |
print(type(weather_data)) | |
print("\n\nRight now in {city} feels like {feels_like}.\n\n" \ | |
.format(city=weather_data["display_location"]["city"], | |
feels_like=weather_data["feelslike_string"] | |
)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment