0

I am very new to using API's and JSON and so far I have been able to request data from an API. I have made a command that, after you input the command and city, will give you weather information for that city. So far I can't get specific data from the data I receive from the API and it makes a list that looks like this after using print(response.content).

b'{"coord":{"lon":-0.1257,"lat":51.5085},"weather":[{"id":804,"main":"Clouds","description":"overcast clouds","icon":"04n"}],"base":"stations","main":{"temp":48.18,"feels_like":40.32,"temp_min":46.99,"temp_max":50,"pressure":1012,"humidity":76},"visibility":10000,"wind":{"speed":10.36,"deg":200},"clouds":{"all":100},"dt":1613450531,"sys":{"type":1,"id":1414,"country":"GB","sunrise":1613459549,"sunset":1613495816},"timezone":0,"id":2643743,"name":"London","cod":200}'

I want to make it output the "feels_like" temperature.

import discord
import os
import requests
import json
from discord.ext import commands
from keep_alive import keep_alive

client = discord.Client()
client = commands.Bot(command_prefix="%")
@client.command()
async def weather(ctx, city):
  #request
  response = requests.get("http://api.openweathermap.org/data/2.5/weather?q=" + city + "&units=imperial&appid=secretkey")


#failed attempts  
#json_data = json.loads(response.text)
#weather_data = json_data[0]['main.feels_like']

 
  #status check + output
  if (response.status_code == 200):
    print("Request successful for " + city + "!")
    await ctx.send("Feels like")
    #await ctx.send(weather_data)
    await ctx.send(response.content)
  else:
    print("Something went wrong retrieving data from weather api for " + city + ".")
    await ctx.send("Couldn't find "+city)
  #output
  print (response.content)

keep_alive()
client.run(os.getenv('daKey'))
DrSanic
  • 11
  • 2
  • `response.content` is a byte string. why is `json_data = json.loads(response.text)` a failed attempt? It creates a python dictionary from which you can extract the data you want. `json_data["main"]["feels_like"]` should get you the value. – Albin Paul Feb 16 '21 at 05:16
  • @AlbinPaul Oh wow thank you so much. I just didn't understand I needed the "main" part in that. How did you know I needed that? Oh nvm I see it now. – DrSanic Feb 16 '21 at 05:26

0 Answers0