Using Python to get data from a Star Wars API
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I am learning Python (about 6 months in, sporadically) and today I decided to write a script to download JSON information from an API.
To start, I wrote a one-page script with a few functions.
The script does the following:
- The user enters the script on the terminal with a search value.
- The script gets the response information from the Star Wars API (https://swapi.co). This is an open API, so I don't need any keys or tokens.
- If the API returns one value, the information is displayed immediately.
- If the API returns several values, the script prompts the user to make a choice. Then the information is displayed.
Right now, the script only works for people in the SWAPI (e.g., Luke Skywalker).
Any feedback on my code would be helpful.
#!/usr/bin/python3
'''Script to get information from https://swapi.co/'''
import requests
import json
import sys
#TODO: Print useful information such as movie titles or names of worlds instead of showing links
#Break up URL to allow for more dynamic searching in the future. Right now, script only handles the people parameter when getting info from the API.
UNDERLINE_LEN = 30
def show(data, index=0):
'''
Displays an individual Swapi object.
data = data retrieved from api
index = which entry to select. Default is 0 for single item lists.
'''
info = data['results'][index]
print()
print('-' * UNDERLINE_LEN)
print("Name : 0n1".format(data['results'][index]['name'], '-' * UNDERLINE_LEN))
for k, v in info.items():
if type(v) is list:
counter = 0
print(k, ':')
for j in v:
print("t0".format(v[counter]))
counter += 1
else:
if k != 'name':
print(k, ':', v)
def show_choices(data):
'''Shows a list of Swapi objects and prompts users to select an object [integer]'''
info = data['results']
counter = 0
for i in info:
print("[0] 1".format(counter + 1, data['results'][counter]['name']))
counter += 1
print()
choice = int(input("Which item would you like to view? "))
try:
show(data, choice - 1)
except IndexError as ie:
print(ie)
def grab():
'''Main function.'''
try:
search = sys.argv[1]
url = 'https://swapi.co/api/people/?search=0'.format(search) #need to divide url to make search process more dynamic
r = requests.get(url)
data = json.loads(r.text)
if data is not None:
numResults = int(data['count'])
txt = "results"
if numResults == 1:
txt = "result"
resText = "0 1 found for search parameter '2'".format(numResults, txt, search)
print(resText)
if numResults == 1:
show(data)
else:
show_choices(data)
except Exception as e:
print(e)
grab()
python json api
add a comment |Â
up vote
3
down vote
favorite
I am learning Python (about 6 months in, sporadically) and today I decided to write a script to download JSON information from an API.
To start, I wrote a one-page script with a few functions.
The script does the following:
- The user enters the script on the terminal with a search value.
- The script gets the response information from the Star Wars API (https://swapi.co). This is an open API, so I don't need any keys or tokens.
- If the API returns one value, the information is displayed immediately.
- If the API returns several values, the script prompts the user to make a choice. Then the information is displayed.
Right now, the script only works for people in the SWAPI (e.g., Luke Skywalker).
Any feedback on my code would be helpful.
#!/usr/bin/python3
'''Script to get information from https://swapi.co/'''
import requests
import json
import sys
#TODO: Print useful information such as movie titles or names of worlds instead of showing links
#Break up URL to allow for more dynamic searching in the future. Right now, script only handles the people parameter when getting info from the API.
UNDERLINE_LEN = 30
def show(data, index=0):
'''
Displays an individual Swapi object.
data = data retrieved from api
index = which entry to select. Default is 0 for single item lists.
'''
info = data['results'][index]
print()
print('-' * UNDERLINE_LEN)
print("Name : 0n1".format(data['results'][index]['name'], '-' * UNDERLINE_LEN))
for k, v in info.items():
if type(v) is list:
counter = 0
print(k, ':')
for j in v:
print("t0".format(v[counter]))
counter += 1
else:
if k != 'name':
print(k, ':', v)
def show_choices(data):
'''Shows a list of Swapi objects and prompts users to select an object [integer]'''
info = data['results']
counter = 0
for i in info:
print("[0] 1".format(counter + 1, data['results'][counter]['name']))
counter += 1
print()
choice = int(input("Which item would you like to view? "))
try:
show(data, choice - 1)
except IndexError as ie:
print(ie)
def grab():
'''Main function.'''
try:
search = sys.argv[1]
url = 'https://swapi.co/api/people/?search=0'.format(search) #need to divide url to make search process more dynamic
r = requests.get(url)
data = json.loads(r.text)
if data is not None:
numResults = int(data['count'])
txt = "results"
if numResults == 1:
txt = "result"
resText = "0 1 found for search parameter '2'".format(numResults, txt, search)
print(resText)
if numResults == 1:
show(data)
else:
show_choices(data)
except Exception as e:
print(e)
grab()
python json api
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am learning Python (about 6 months in, sporadically) and today I decided to write a script to download JSON information from an API.
To start, I wrote a one-page script with a few functions.
The script does the following:
- The user enters the script on the terminal with a search value.
- The script gets the response information from the Star Wars API (https://swapi.co). This is an open API, so I don't need any keys or tokens.
- If the API returns one value, the information is displayed immediately.
- If the API returns several values, the script prompts the user to make a choice. Then the information is displayed.
Right now, the script only works for people in the SWAPI (e.g., Luke Skywalker).
Any feedback on my code would be helpful.
#!/usr/bin/python3
'''Script to get information from https://swapi.co/'''
import requests
import json
import sys
#TODO: Print useful information such as movie titles or names of worlds instead of showing links
#Break up URL to allow for more dynamic searching in the future. Right now, script only handles the people parameter when getting info from the API.
UNDERLINE_LEN = 30
def show(data, index=0):
'''
Displays an individual Swapi object.
data = data retrieved from api
index = which entry to select. Default is 0 for single item lists.
'''
info = data['results'][index]
print()
print('-' * UNDERLINE_LEN)
print("Name : 0n1".format(data['results'][index]['name'], '-' * UNDERLINE_LEN))
for k, v in info.items():
if type(v) is list:
counter = 0
print(k, ':')
for j in v:
print("t0".format(v[counter]))
counter += 1
else:
if k != 'name':
print(k, ':', v)
def show_choices(data):
'''Shows a list of Swapi objects and prompts users to select an object [integer]'''
info = data['results']
counter = 0
for i in info:
print("[0] 1".format(counter + 1, data['results'][counter]['name']))
counter += 1
print()
choice = int(input("Which item would you like to view? "))
try:
show(data, choice - 1)
except IndexError as ie:
print(ie)
def grab():
'''Main function.'''
try:
search = sys.argv[1]
url = 'https://swapi.co/api/people/?search=0'.format(search) #need to divide url to make search process more dynamic
r = requests.get(url)
data = json.loads(r.text)
if data is not None:
numResults = int(data['count'])
txt = "results"
if numResults == 1:
txt = "result"
resText = "0 1 found for search parameter '2'".format(numResults, txt, search)
print(resText)
if numResults == 1:
show(data)
else:
show_choices(data)
except Exception as e:
print(e)
grab()
python json api
I am learning Python (about 6 months in, sporadically) and today I decided to write a script to download JSON information from an API.
To start, I wrote a one-page script with a few functions.
The script does the following:
- The user enters the script on the terminal with a search value.
- The script gets the response information from the Star Wars API (https://swapi.co). This is an open API, so I don't need any keys or tokens.
- If the API returns one value, the information is displayed immediately.
- If the API returns several values, the script prompts the user to make a choice. Then the information is displayed.
Right now, the script only works for people in the SWAPI (e.g., Luke Skywalker).
Any feedback on my code would be helpful.
#!/usr/bin/python3
'''Script to get information from https://swapi.co/'''
import requests
import json
import sys
#TODO: Print useful information such as movie titles or names of worlds instead of showing links
#Break up URL to allow for more dynamic searching in the future. Right now, script only handles the people parameter when getting info from the API.
UNDERLINE_LEN = 30
def show(data, index=0):
'''
Displays an individual Swapi object.
data = data retrieved from api
index = which entry to select. Default is 0 for single item lists.
'''
info = data['results'][index]
print()
print('-' * UNDERLINE_LEN)
print("Name : 0n1".format(data['results'][index]['name'], '-' * UNDERLINE_LEN))
for k, v in info.items():
if type(v) is list:
counter = 0
print(k, ':')
for j in v:
print("t0".format(v[counter]))
counter += 1
else:
if k != 'name':
print(k, ':', v)
def show_choices(data):
'''Shows a list of Swapi objects and prompts users to select an object [integer]'''
info = data['results']
counter = 0
for i in info:
print("[0] 1".format(counter + 1, data['results'][counter]['name']))
counter += 1
print()
choice = int(input("Which item would you like to view? "))
try:
show(data, choice - 1)
except IndexError as ie:
print(ie)
def grab():
'''Main function.'''
try:
search = sys.argv[1]
url = 'https://swapi.co/api/people/?search=0'.format(search) #need to divide url to make search process more dynamic
r = requests.get(url)
data = json.loads(r.text)
if data is not None:
numResults = int(data['count'])
txt = "results"
if numResults == 1:
txt = "result"
resText = "0 1 found for search parameter '2'".format(numResults, txt, search)
print(resText)
if numResults == 1:
show(data)
else:
show_choices(data)
except Exception as e:
print(e)
grab()
python json api
edited Jan 26 at 23:46
200_success
123k14143401
123k14143401
asked Jan 26 at 23:13
Michael Darnell
162
162
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
5
down vote
You should never compose URLs using this technique!
search = sys.argv[1]
url = 'https://swapi.co/api/people/?search=0'.format(search) #need to divide url to make search process more dynamic
r = requests.get(url)
Query string parameters need to be percent-encoded. The Python Requests library provides a correct way to do it:
search = sys.argv[1]
url = 'https://swapi.co/api/people/'
r = requests.get(url, params='search': search)
Going further, I would caution you to never concatenate any user-supplied string to form another string that will be interpreted by some computer system. You must first considering the escaping mechanism. SQL injections, cross-site scripting, LDAP injection, etc. â they're all vulnerabilities that result from this same kind of carelessness.
1
Thanks for the feedback. I saw a few params examples but didn't understand them. Well noted on the concatenation.
â Michael Darnell
Jan 27 at 0:13
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
5
down vote
You should never compose URLs using this technique!
search = sys.argv[1]
url = 'https://swapi.co/api/people/?search=0'.format(search) #need to divide url to make search process more dynamic
r = requests.get(url)
Query string parameters need to be percent-encoded. The Python Requests library provides a correct way to do it:
search = sys.argv[1]
url = 'https://swapi.co/api/people/'
r = requests.get(url, params='search': search)
Going further, I would caution you to never concatenate any user-supplied string to form another string that will be interpreted by some computer system. You must first considering the escaping mechanism. SQL injections, cross-site scripting, LDAP injection, etc. â they're all vulnerabilities that result from this same kind of carelessness.
1
Thanks for the feedback. I saw a few params examples but didn't understand them. Well noted on the concatenation.
â Michael Darnell
Jan 27 at 0:13
add a comment |Â
up vote
5
down vote
You should never compose URLs using this technique!
search = sys.argv[1]
url = 'https://swapi.co/api/people/?search=0'.format(search) #need to divide url to make search process more dynamic
r = requests.get(url)
Query string parameters need to be percent-encoded. The Python Requests library provides a correct way to do it:
search = sys.argv[1]
url = 'https://swapi.co/api/people/'
r = requests.get(url, params='search': search)
Going further, I would caution you to never concatenate any user-supplied string to form another string that will be interpreted by some computer system. You must first considering the escaping mechanism. SQL injections, cross-site scripting, LDAP injection, etc. â they're all vulnerabilities that result from this same kind of carelessness.
1
Thanks for the feedback. I saw a few params examples but didn't understand them. Well noted on the concatenation.
â Michael Darnell
Jan 27 at 0:13
add a comment |Â
up vote
5
down vote
up vote
5
down vote
You should never compose URLs using this technique!
search = sys.argv[1]
url = 'https://swapi.co/api/people/?search=0'.format(search) #need to divide url to make search process more dynamic
r = requests.get(url)
Query string parameters need to be percent-encoded. The Python Requests library provides a correct way to do it:
search = sys.argv[1]
url = 'https://swapi.co/api/people/'
r = requests.get(url, params='search': search)
Going further, I would caution you to never concatenate any user-supplied string to form another string that will be interpreted by some computer system. You must first considering the escaping mechanism. SQL injections, cross-site scripting, LDAP injection, etc. â they're all vulnerabilities that result from this same kind of carelessness.
You should never compose URLs using this technique!
search = sys.argv[1]
url = 'https://swapi.co/api/people/?search=0'.format(search) #need to divide url to make search process more dynamic
r = requests.get(url)
Query string parameters need to be percent-encoded. The Python Requests library provides a correct way to do it:
search = sys.argv[1]
url = 'https://swapi.co/api/people/'
r = requests.get(url, params='search': search)
Going further, I would caution you to never concatenate any user-supplied string to form another string that will be interpreted by some computer system. You must first considering the escaping mechanism. SQL injections, cross-site scripting, LDAP injection, etc. â they're all vulnerabilities that result from this same kind of carelessness.
answered Jan 26 at 23:55
200_success
123k14143401
123k14143401
1
Thanks for the feedback. I saw a few params examples but didn't understand them. Well noted on the concatenation.
â Michael Darnell
Jan 27 at 0:13
add a comment |Â
1
Thanks for the feedback. I saw a few params examples but didn't understand them. Well noted on the concatenation.
â Michael Darnell
Jan 27 at 0:13
1
1
Thanks for the feedback. I saw a few params examples but didn't understand them. Well noted on the concatenation.
â Michael Darnell
Jan 27 at 0:13
Thanks for the feedback. I saw a few params examples but didn't understand them. Well noted on the concatenation.
â Michael Darnell
Jan 27 at 0:13
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f186099%2fusing-python-to-get-data-from-a-star-wars-api%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password