Skip to content

Instantly share code, notes, and snippets.

@bohnacker
Last active December 20, 2018 10:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bohnacker/10875f47f568dd4bfa36d29a0e3625a0 to your computer and use it in GitHub Desktop.
Save bohnacker/10875f47f568dd4bfa36d29a0e3625a0 to your computer and use it in GitHub Desktop.
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