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 to output one value per request of the CSV and print it in another func? Student44 3 1,278 Nov-11-2022, 10:45 PM
Last Post: snippsat
  How to combine two output in one variable? ilknurg 2 1,144 Aug-01-2022, 09:31 AM
Last Post: Gribouillis
  Os command output in variable shows wrong value paulo79 2 1,467 Apr-09-2022, 03:48 PM
Last Post: ndc85430
  Search text in PDF and output its page number. atomxkai 21 8,675 Jan-21-2022, 06:20 AM
Last Post: snippsat
  how can I correct the Bad Request error on my curl request tomtom 8 4,968 Oct-03-2021, 06:32 AM
Last Post: tomtom
  Command output to Variable ironclaw 1 1,742 Aug-26-2021, 06:55 PM
Last Post: bowlofred
  Increment text files output and limit contains Kaminsky 1 3,135 Jan-30-2021, 06:58 PM
Last Post: bowlofred
  Variable scope - "global x" didn't work... ptrivino 5 2,979 Dec-28-2020, 04:52 PM
Last Post: ptrivino
  How to Split Output Audio on Text to Speech Code Base12 2 6,784 Aug-29-2020, 03:23 AM
Last Post: Base12
  ImportError: cannot import name 'Request' from 'request' abhishek81py 1 3,860 Jun-18-2020, 08:07 AM
Last Post: buran

Forum Jump:

User Panel Messages

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