Advertisement
Guest User

Untitled

a guest
Oct 26th, 2016
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.21 KB | None | 0 0
  1. import urllib
  2. import json
  3.  
  4. request = urlopen("http://api.exmaple.com/stuff?client_id=someid&client_secret=randomsecret")
  5. response = request.read()
  6. json = json.loads(response)
  7. if json['success']:
  8. ob = json['response']['ob']
  9. print ("The current weather in Seattle is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF'])
  10. else:
  11. print ("An error occurred: %s") % (json['error']['description'])
  12. request.close()
  13.  
  14. Traceback (most recent call last):
  15. File "thing.py", line 4, in <module>
  16. request = urlopen("http://api.exmaple.com/stuff?client_id=someid&client_secret=randomsecret")
  17. NameError: name 'urlopen' is not defined
  18.  
  19. from urllib.request import urlopen
  20. req = urlopen(...)
  21.  
  22. import urllib.request
  23. req = request.urlopen(...)
  24.  
  25. from urllib import urlopen
  26.  
  27. import urllib
  28. import json
  29.  
  30. request = urllib.urlopen("http://api.exmaple.com/stuff?client_id=someid&client_secret=randomsecret")
  31. response = request.read()
  32. json = json.loads(response)
  33.  
  34. if json['success']:
  35. ob = json['response']['ob']
  36. print ("The current weather in Seattle is %s with a temperature of %d") % (ob['weather'].lower(), ob['tempF'])
  37.  
  38. else:
  39. print ("An error occurred: %s") % (json['error']['description'])
  40.  
  41. request.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement