Advertisement
Guest User

Python JSON sample

a guest
Jul 22nd, 2014
310
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.44 KB | None | 0 0
  1. # Garph from reddit's example code for importing JSON
  2.  
  3. import JSON
  4. from urllib import requests
  5.  
  6. def getJSON(url):
  7. # Retrieves JSON data from a website
  8. # error handling included for invalid URLs
  9.     try:
  10.         stream = request.urlopen(url)
  11.  
  12.         if stream.getcode() == 200:
  13.             data = stream.read()
  14.             data = data.decode("utf-8")
  15.             return json.loads(data)
  16.  
  17.         else:
  18.             print("URL Unsucessful")
  19.  
  20.     except request.HTTPError:
  21.         print("Invalid URL")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement