Gabriel Carneiro
Gabriel Carneiro

Reputation: 85

How can I iterate the entries of an API?

I have this api : https://api.publicapis.org/entries

And I wat to iterate key entries of it. I tried as it follows :

r = requests.get('https://api.publicapis.org/entries') 
    entries = [] #initializing the vector entries
    for i in entries: #iterating it
        return(i.text) #trying to print the entries

Then I received the following error :
TypeError: The view function did not return a valid response. The function either returned None or ended without a return statement.

How can I solve this problem ?

Upvotes: 0

Views: 197

Answers (2)

Ahmed Mamdouh
Ahmed Mamdouh

Reputation: 706

You can use json.loads to parse response text.

Let me add the full code.

import requests
import json
r = requests.get('https://api.publicapis.org/entries')
entries = json.loads(r.text)['entries'] #initializing the vector entries
for i in entries: #iterating it
    API = i['API']
    Description = i['Description']

Upvotes: 0

AKX
AKX

Reputation: 168913

For that particular API endpoint, you should be fine with

resp = requests.get('https://api.publicapis.org/entries') 
resp.raise_for_status()  # raise exception on HTTP errors
entries = resp.json()["entries"]  # parse JSON, dig out the entries list
# ... do something with entries.

Upvotes: 1

Related Questions