Requests Module is great python module to use urllib2 in easy ways. For instance, let we get some JSON from my Gravatar :
1 | https://es.gravatar.com/bashlook.json |
In case you need how to get JSON value using urllib2 :
1 2 3 4 5 6 7 8 9 10 11 | import urllib2 import json uri = "https://es.gravatar.com/bashlook.json" opener = urllib2.urlopen(uri) try: result = json.load(opener) except ValueError, e: errorMessage = str(result) + ‘:’ + str(e) return errorMessage |
At this cases, “result” can be accessed as dictionaries, example :
1 | result[‘entry’] |
Now, we try with requests module :
1 2 3 4 5 6 7 8 9 10 11 | import requests import json uri = "https://es.gravatar.com/bashlook.json" try: resp = requests.get(uri) except requests.ConnectionError: print "Connection Error" content = json.loads(resp.content) |
Requests module throw JSON String in resp.content. We need to convert JSON string into dictionaries by :
1 | json.loads(resp.content) |
The differences :
1. json.load is decode JSON File.
2. json.loads is decode JSON String / JSON Objects.