I'm new here and need your help for my master's thesis. I need to save data from a charger web interface to an Excel file. So far I can call up the web interface and can also save the data in an Excel list. But I always get a "0.00" as the return value for voltage and current, although the values are constantly changing.
Here is my code what I wrote. And a screenshot of the web interface.
I thank you in advance for your support
import csv
import time
import requests
from bs4 import BeautifulSoup
import lxml
import datetime
# Hier den Wert end = '13.7' eintragen! für Spannung
end = '13.67'
def holedaten():
url = ('http://mcb:8080/')
r = requests.get(url, auth=('admin', 'mcb'))
web_content = BeautifulSoup(r.content, 'html.parser')
#time.sleep(1)
zeitaktuell = datetime.datetime.now()
datum = zeitaktuell.strftime("%d-%m-%Y")
uhrzeit = zeitaktuell.strftime("%H:%M:%S")
# Udev - Spannung vom Ladegerät
Udev = web_content.find(id='voltageset').text
# Ubatt - Spannung Batterie
Ubatt = web_content.find(id='voltage').text
# Ibatt - Strom Batterie
Ibatt = web_content.find(id='current').text
# Ahges - Ah-Zähler
Ahges = web_content.find(id='MCBAmpere').text
# mcbdauer - Aktuelle Dauer
mcbdauer = web_content.find(id='MCBRunTime').text
meineliste = [datum, uhrzeit, Udev, Ubatt, Ibatt, Ahges, mcbdauer]
#print(meineliste)
#print(Ahges)
#print(mcbdauer)
return meineliste
with open('real_data.csv', 'w', newline='')as csvfile:
condition = True
writer = csv.writer(csvfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
tst = ['Datum', 'Uhrzeit', 'Udev', 'Ubatt', 'Ibatt', 'Ahges', 'mcbdauer', 'Identdauer', 'Batterie']
writer.writerow(tst)
while condition == True:
daten = holedaten()
# daten(2) wäre Udev; 0=datum 1=Uhrzeit 2=Udev 3=Ubatt...
bis = daten[3]
writer.writerow(daten)
print(daten)
#if bis <= end:
# condition = False
#print("Wert erreicht!")
My results:
['14-10-2021', '10:59:01', '0.00', '0.00', '0.00', '', '']
['14-10-2021', '10:59:05', '0.00', '0.00', '0.00', '', '']
['14-10-2021', '10:59:08', '0.00', '0.00', '0.00', '', '']
['14-10-2021', '10:59:11', '0.00', '0.00', '0.00', '', '']
['14-10-2021', '10:59:14', '0.00', '0.00', '0.00', '', '']
['14-10-2021', '10:59:17', '0.00', '0.00', '0.00', '', '']
Here is the Screenshot.
voltage and current values Amperehours and Runtime
CodePudding user response:
Look for the direct request for that data. Try:
import csv
import time
import requests
from bs4 import BeautifulSoup
import lxml
import datetime
# Hier den Wert end = '13.7' eintragen! für Spannung
end = '13.67'
def holedaten():
url = ('http//mcb:8080/MCBStatus.xml')
xml_response = requests.get(url, auth=('admin', 'mcb'))
xml = xml_response.text
xml_content = BeautifulSoup(xml, 'lxml')
clock = xml_content.find('clock').text
datum = clock.split()[0] # <--
uhrzeit = clock.split()[-1] # <--
# Udev - Spannung vom Ladegerät
Udev = xml_content.find('voltageset').text # <--
# Ubatt - Spannung Batterie
Ubatt = xml_content.find('voltage').text # <--
# Ibatt - Strom Batterie
Ibatt = xml_content.find('current').text # <--
# Ahges - Ah-Zähler
Ahges = xml_content.find('mcbampere').text
# mcbdauer - Aktuelle Dauer
mcbdauer = xml_content.find('mcbruntime').text
meineliste = [datum, uhrzeit, Udev, Ubatt, Ibatt, Ahges, mcbdauer]
#print(meineliste)
#print(Ahges)
#print(mcbdauer)
return meineliste
with open('real_data.csv', 'w', newline='')as csvfile:
condition = True
writer = csv.writer(csvfile, delimiter=';', quotechar='"', quoting=csv.QUOTE_MINIMAL)
tst = ['Datum', 'Uhrzeit', 'Udev', 'Ubatt', 'Ibatt', 'Ahges', 'mcbdauer', 'Identdauer', 'Batterie']
writer.writerow(tst)
while condition == True:
daten = holedaten()
# daten(2) wäre Udev; 0=datum 1=Uhrzeit 2=Udev 3=Ubatt...
bis = daten[3]
writer.writerow(daten)
print(daten)
#if bis <= end:
# condition = False
#print("Wert erreicht!")
#f1 = open("inFile", "r") # open input file for reading
print('finished')