0

I am trying to acces data from the pokemon api and get some information from there. First you need to put in a number (only option 1 works right now). Then the pokemon name/number and the url is formed. but I get a lot of errors when

data = json.loads(THEURL)
print(data['forms']['name'])

What am I doing wrong?

#importception
import requests
import json

#import poke api
api_url = 'http://pokeapi.co/api/v2/'

#welke database
def databaseaanvragen():
    print('Welke database wilt u aanvragen?')
    print('Hallo), wat wilt u doen?')
    print('1) Pokemons')
    print('2) Locations')
    print('3) Moves')
    keuze = input('Voer hier het nummer in: ')
    if(keuze.isdigit()):
        keuze = int(keuze)

    if keuze == 1:
        poke_naam = input('Voer de naam of het nummer van de Pokemon in: ')
        poke_value = 'pokemon/' + poke_naam
        return poke_value

#database url en data aanvragen
aangevraagd = api_url + databaseaanvragen()
data = json.loads(aangevraagd)

print(data['forms']['name'])
1
  • 2
    json.loads expects a string containing the JSON to be decoded, not a URL. json.load instead expects an open file pointer. In short: the json module will not do an actual HTTP request to get the JSON document, that's not its job; you'll need to do that yourself. Commented Oct 21, 2016 at 13:53

1 Answer 1

1

Doesn't actually look like you are querying the JSON api with a GET request at all. You'll need code that looks something like the following:

try:
    r = requests.get(api_url)
except requests.exceptions.RequestException as error:
    print(error)
    sys.exit(1)
else:
   # No Exception was raised :)
   # Decode the JSON from the response content
   json_data = r.json()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.