GET Request to Server
Lição 9 de 10 do curso API em Python da Coddy.
Desafio
MédioAgora que você estabeleceu uma conexão adequada com uma API, vamos enviar uma requisição GET para um local específico.
- Envie uma requisição GET para:
https://wttr.in/London?format=j1(a parte?format=j1diz ao serviço para retornar JSON) - Salve o objeto
requests.get()em uma variável chamadaresponse - Analise a resposta usando
response.json()e salve-a em uma variável chamadadata - As condições atuais estão dentro de
data["current_condition"][0]. Extraia a temperatura (temp_C) e a descrição (weatherDesc[0]["value"]), e então imprima ambas
Experimente você mesmo
import requests
# Step 1: Send GET request for London weather (j1 = JSON format)
response = # Your code here
# Step 2: Parse JSON response
data = # Your code here
# Step 3: Print weather information
current = data["current_condition"][0]
print("Temperature:", current["temp_C"], "°C")
print("Description:", current["weatherDesc"][0]["value"])Todas as lições de API em Python
3Weather Fetch API Program
API Connection to ServerGET Request to ServerDisplaying Appropriate Results