HTTP requests and JSON parsing in Python
Become part of the top 3% of the developers by applying to Toptal https://topt.al/25cXVn
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Dreaming in Puzzles
--
Chapters
00:00 Question
00:43 Accepted answer (Score 489)
01:11 Answer 2 (Score 168)
01:40 Answer 3 (Score 48)
01:54 Answer 4 (Score 32)
02:07 Thank you
--
Full question
https://stackoverflow.com/questions/6386...
Question links:
[http://maps.googleapis.com/maps/api/dire...]: http://maps.googleapis.com/maps/api/dire...
[in the JSON format]: https://developers.google.com/maps/docum...
Accepted answer links:
[requests]: https://requests.readthedocs.io/en/maste.../
https://requests.readthedocs.io/en/maste...
Answer 2 links:
[requests]: https://requests.readthedocs.io/en/lates.../
[the module's documentation]: https://requests.readthedocs.io/en/maste...
Answer 3 links:
[requests]: http://docs.python-requests.org/en/lates...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #json
#avk47
--
Music by Eric Matyas
https://www.soundimage.org
Track title: Dreaming in Puzzles
--
Chapters
00:00 Question
00:43 Accepted answer (Score 489)
01:11 Answer 2 (Score 168)
01:40 Answer 3 (Score 48)
01:54 Answer 4 (Score 32)
02:07 Thank you
--
Full question
https://stackoverflow.com/questions/6386...
Question links:
[http://maps.googleapis.com/maps/api/dire...]: http://maps.googleapis.com/maps/api/dire...
[in the JSON format]: https://developers.google.com/maps/docum...
Accepted answer links:
[requests]: https://requests.readthedocs.io/en/maste.../
https://requests.readthedocs.io/en/maste...
Answer 2 links:
[requests]: https://requests.readthedocs.io/en/lates.../
[the module's documentation]: https://requests.readthedocs.io/en/maste...
Answer 3 links:
[requests]: http://docs.python-requests.org/en/lates...
--
Content licensed under CC BY-SA
https://meta.stackexchange.com/help/lice...
--
Tags
#python #json
#avk47
ACCEPTED ANSWER
Score 507
I recommend using the awesome requests library:
import requests
url = 'http://maps.googleapis.com/maps/api/directions/json'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
resp = requests.get(url=url, params=params)
data = resp.json() # Check the JSON Response Content documentation below
JSON Response Content: https://requests.readthedocs.io/en/master/user/quickstart/#json-response-content
ANSWER 2
Score 174
The requests Python module takes care of both retrieving JSON data and decoding it, due to its builtin JSON decoder. Here is an example taken from the module's documentation:
>>> import requests
>>> r = requests.get('https://github.com/timeline.json')
>>> r.json()
[{u'repository': {u'open_issues': 0, u'url': 'https://github.com/...
So there is no use of having to use some separate module for decoding JSON.
ANSWER 3
Score 33
import urllib
import json
url = 'http://maps.googleapis.com/maps/api/directions/json?origin=Chicago,IL&destination=Los+Angeles,CA&waypoints=Joplin,MO|Oklahoma+City,OK&sensor=false'
result = json.load(urllib.urlopen(url))
ANSWER 4
Score 22
Use the requests library, pretty print the results so you can better locate the keys/values you want to extract, and then use nested for loops to parse the data. In the example I extract step by step driving directions.
import json, requests, pprint
url = 'http://maps.googleapis.com/maps/api/directions/json?'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
data = requests.get(url=url, params=params)
binary = data.content
output = json.loads(binary)
# test to see if the request was valid
#print output['status']
# output all of the results
#pprint.pprint(output)
# step-by-step directions
for route in output['routes']:
for leg in route['legs']:
for step in leg['steps']:
print step['html_instructions']