Python Forum
Getting a GET request output text into a variable to work with it. - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Getting a GET request output text into a variable to work with it. (/thread-32658.html)



Getting a GET request output text into a variable to work with it. - LeoT - Feb-24-2021

Hi there,
I'm an total beginner in python and currently experimenting with the request library.
I basically want to do a GET request to my local solar inverter to work with its data.
I already got that to work and received the following data:
{
   "Body" : {
      "Data" : {
         "Inverters" : {
            "1" : {
               "DT" : 105,
               "E_Day" : 20781,
               "E_Total" : 309730,
               "E_Year" : 309730.8125,
               "P" : 3681
            }
         },
         "Site" : {
            "E_Day" : 20781,
            "E_Total" : 309730,
            "E_Year" : 309730.8125,
            "Meter_Location" : "grid",
            "Mode" : "meter",
            "P_Akku" : null,
            "P_Grid" : 650.5,
            "P_Load" : -4331.5,
            "P_PV" : 3681,
            "rel_Autonomy" : 84.982107814844738,
            "rel_SelfConsumption" : 100
         },
         "Version" : "12"
      }
   },
   "Head" : {
      "RequestArguments" : {},
      "Status" : {
         "Code" : 0,
         "Reason" : "",
         "UserMessage" : ""
      },
      "Timestamp" : "2021-02-24T14:26:59+01:00"
   }
}
Now the problem starts :) Although I see the data well displayed in my terminal I can't get it to save to a variable.
I basically want to save the data behind "P_Grid" : 650.5, "P_Load" : -4331.5, and "P_PV" : 3681 into a variable to work with it.
I thought it would be quite easy but after searching for hours on the internet I couldn't find a solution. It shouldn't be that hard am I right?! Huh

That is the simple code I'm getting the data from:
import requests

Inverter_Local_IP = '192.168.XXX.XXX' #Local IP of your Fronius Inverter

InverterData = requests.get('http://%s/solar_api/v1/GetPowerFlowRealtimeData.fcgi' % Inverter_Local_IP) #Retrieving data from your Inverter

print(InverterData.text) #Displaying the retrieved data
I hope someone can help me.
Thanks in advance!
Best,
Leo


RE: Getting a GET request output text into a variable to work with it. - snippsat - Feb-24-2021

Usually get json back as you data look like.
So like this,some style change and f-string advise to.
import requests

inverter_local_ip = '192.168.XXX.XXX'
inverter_data = requests.get(f'http://{inverter_local_ip}/solar_api/v1/GetPowerFlowRealtimeData.fcgi')
json_data = inverter_data.json()
If this work then data will now be a Python dictionary.
>>> json_data['Body']['Data']['Site']['P_Grid']
650.5
>>> json_data['Body']['Data']['Site']['P_Load']
-4331.5



RE: Getting a GET request output text into a variable to work with it. - LeoT - Feb-24-2021

(Feb-24-2021, 01:58 PM)snippsat Wrote: Usually get json back as you data look like.
So like this,some style change and f-string advise to.
import requests

inverter_local_ip = '192.168.XXX.XXX'
inverter_data = requests.get(f'http://{inverter_local_ip}/solar_api/v1/GetPowerFlowRealtimeData.fcgi')
json_data = inverter_data.json()
If this work then data will now be a Python dictionary.
>>> json_data['Body']['Data']['Site']['P_Grid']
650.5
>>> json_data['Body']['Data']['Site']['P_Load']
-4331.5

Thanks a lot! You saved my day!!! Smile Smile Smile

Like that I can work with it perfectly!

import requests

inverter_local_ip = '192.168.178.174' #Local IP of your Fronius Inverter

inverter_data = requests.get('http://%s/solar_api/v1/GetPowerFlowRealtimeData.fcgi' % inverter_local_ip) #Retrieving data from your Inverter
json_data = inverter_data.json()
#print(inverter_data.text) #Displaying the retrieved data

print(json_data['Body']['Data']['Site']['P_Grid'])
print(json_data['Body']['Data']['Site']['P_Load'])
print(json_data['Body']['Data']['Site']['P_PV'])