Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Python script to get JSON data from an online source. Seems to work only with Python 2.
import urllib2
import json
opener = urllib2.build_opener()
# An object to collect the results
result = {'episodes':[]}
# Iterate from 1 to 31
for i in range(1, 32):
# Get the data
req = urllib2.Request("https://rickandmortyapi.com/api/episode/" + str(i))
f = opener.open(req)
res = f.read()
# Convert it to JSON
obj = json.loads(res)
# Append the data to the result
result['episodes'].append(obj)
# Write it to a file
writeFile = open('episodes.json', 'wb')
writeFile.write(json.dumps(result))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment