Last active
December 20, 2018 10:38
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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