How to return a list with a certain parameter from an API?

Question:

I need to return a list with the the title ("titulo") of the news that appear in this api: https://www.publico.pt/api/list/ultimas

I tried this but it only returns the title of the title (titulo) of the first new and not all the titles.

import requests

def get_news():
    
    url = "https://www.publico.pt/api/list/ultimas"

    response = requests.get(url)
    data = response.json()  
    for news in data:
        titulo = [news["titulo"]]  
        
        return titulo
    
print(get_news())

Asked By: Manuel Carvalhinha

||

Answers:

i think this might be what you actually want to do:

url = "https://www.publico.pt/api/list/ultimas"

response = requests.get(url)
data = response.json()
titulo = []
for news in data:    
    titulo.append(news["titulo"])  
    
return titulo

this puts all the data into a list and returns the list.

Answered By: maxxel_
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.