Python Forum
Getting a GET request output text into a variable to work with it.
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Getting a GET request output text into a variable to work with it.
#1
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
Reply
#2
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
LeoT likes this post
Reply
#3
(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'])
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  how can I correct the Bad Request error on my curl request tomtom 8 347 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Command output to Variable ironclaw 1 358 Aug-26-2021, 06:55 PM
Last Post: bowlofred
  Increment text files output and limit contains Kaminsky 1 868 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  Variable scope - "global x" didn't work... ptrivino 5 858 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  How to Split Output Audio on Text to Speech Code Base12 2 3,771 Aug-29-2020, 03:23 AM
Last Post: Base12
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 1,786 Jun-18-2020, 08:07 AM
Last Post: buran
  Import Text, output curve geometry Alyner 0 838 Feb-03-2020, 03:05 AM
Last Post: Alyner
  get docstring to work with another block of text at the top Skaperen 4 957 Dec-28-2019, 10:37 PM
Last Post: Skaperen
  Capture grep output to a variable which includes another variable kdefilip2 4 2,388 Nov-24-2019, 12:30 PM
Last Post: kdefilip2
  Four text files input combinations into an output file mhyga 2 1,053 Jul-28-2019, 06:52 PM
Last Post: ThomasL

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020