Page 1 sur 3
récupéer des données sur une page WEB simplement
Publié : 19 mai 2018, 10:11
par marcus_54
Bonjour,
Cela fait un moment que je cherche à récupérer des données contenues sur une page WEB le plus simplement possible. Je suis débutant en programmation. J'ai bien trouvé des exemples, mais c'est toujours soit assez compliqué...
J'ai un boitier sur le réseau en 192.168.0.7 qui me fournit une page web contenant la production solaire.
Je ne peux pas modifier ces pages, je ne peux que les consulter.
J'aimerais récupérer par exemple
avec un script lua toutes les minutes la donnée de puissance (ici 606 W)
Code : Tout sélectionner
...
<tr>
<th scope="row">Last System Power</th>
<td>606 W </td>
</tr>
Est-ce que vous pouvez m'aider ?
Merci
Re: récupérer des données sur une page WEB simplement
Publié : 20 mai 2018, 15:14
par papoo
marcus_54 a écrit :Bonjour,
Cela fait un moment que je cherche à récupérer des données contenues sur une page WEB le plus simplement possible. Je suis débutant en programmation. J'ai bien trouvé des exemples, mais c'est toujours soit assez compliqué...
J'ai un boitier sur le réseau en 192.168.0.7 qui me fournit une page web contenant la production solaire.
Je ne peux pas modifier ces pages, je ne peux que les consulter.
J'aimerais récupérer par exemple
avec un script lua toutes les minutes la donnée de puissance (ici 606 W)
Code : Tout sélectionner
...
<tr>
<th scope="row">Last System Power</th>
<td>606 W </td>
</tr>
Est-ce que vous pouvez m'aider ?
Merci
et si pour que l'on puisse t'aider tu nous fournissais le code source de ta page html? ce serait déjà un bon début
Re: récupéer des données sur une page WEB simplement
Publié : 20 mai 2018, 17:10
par spiwit
En bash
Code : Tout sélectionner
#/bin/bash
url="http://localhost/test.html"
valeur=`curl -s $url | grep -A1 '<th scope="row">Last System Power</th>' | tail -n 1 | awk -F " " '{print $1}' | sed s/'<td>'//g`
echo $valeur
Re: récupéer des données sur une page WEB simplement
Publié : 20 mai 2018, 17:57
par marcus_54
spiwit, Je vais essayer le bash
Papoo, je sais que tu es le grand spécialiste des scripts lua, j’ai failli t’envoyer cette demande en MP
Voici le code source de la page
La donnée qui m'intéresse est à la ligne 116 "419 W"
Les ???? sont des caractères chinois
Code : Tout sélectionner
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<!-- ??IE -->
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<!-- ??????????? -->
<meta name="renderer" content="webkit">
<!-- ????? -->
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Altenergy Power Control Software</title>
<link type="image/x-icon" href="http://192.168.0.6/resources/images/favicon.ico" rel="shortcut icon">
<link href="http://192.168.0.6/resources/css/bootstrap.min.css" rel="stylesheet">
<link href="http://192.168.0.6/resources/css/ecu-style.css" rel="stylesheet">
<link href="http://192.168.0.6/resources/css/bootstrapValidator.css" rel="stylesheet">
<!--[if lt IE 8]>
<link href="http://192.168.0.6/resources/css/bootstrap-ie7.css" rel="stylesheet">
<![endif]-->
<script src="http://192.168.0.6/resources/js/jquery-1.8.2.min.js"></script>
<script src="http://192.168.0.6/resources/js/bootstrap.min.js"></script>
<script src="http://192.168.0.6/resources/js/bootstrapValidator.min.js"></script>
<!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
<!-- WARNING: Respond.js doesn"t work if you view the page via file:// -->
<!--[if lt IE 9]>
<script src="js/html5shiv.min.js"></script>
<script src="js/respond.min.js"></script>
<![endif]-->
</head>
<body>
<!-- ????? -->
<header>
<div class="navbar navbar-default navbar-top">
<div class="container">
<div class="navbar-header">
<button class="navbar-toggle" data-target="#navbar-header" data-toggle="collapse" type="button">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="http://www.altenergy-power.com">
<img src="http://192.168.0.6/resources/images/logo.png">
</a>
</div>
<div class="navbar-collapse collapse" id="navbar-header">
<ul class="nav navbar-nav navbar-title">
<li><a id="ecu_title">ENERGY COMMUNICATION UNIT</a></li>
</ul>
<ul class="nav navbar-nav navbar-right">
<a class="btn chlang" id="english" >English</a>|
<a class="btn chlang" id="chinese" >Chinese</a>
</ul>
</div>
</div>
</div>
</header><!-- ????? -->
<nav>
<div class="navbar navbar-default navbar-menu">
<div class="container">
<p class="navbar-menu-title">Home</p>
<div class="navbar-header">
<button class="navbar-toggle" data-target="#navbar-menu" data-toggle="collapse" type="button">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
</div>
<div class="navbar-collapse collapse" id="navbar-menu">
<ul class="nav navbar-nav ">
<li><a href="http://192.168.0.6/index.php/home" class="active">Home</a></li>
<li><a href="http://192.168.0.6/index.php/realtimedata">Real Time Data</a><span> </span></li>
<li><a href="http://192.168.0.6/index.php/management">Administration</a><span> </span></li>
</ul>
</div>
</div>
</div>
</nav>
<section>
<div class="container container-main">
<!-- ????? -->
<aside class="col-md-3 col-md-push-9">
<div class="list-group">
<a id="ecu_time" class="list-group-item align-center" ></a>
<a class="list-group-item active align-center">ENVIRONMENTAL BENEFITS</a>
<a class="list-group-item benefit align-center">CO<sub>2</sub> Offset Equivalent to</a>
<a class="list-group-item benefit"><img src="http://192.168.0.6/resources/images/car.png"><div class="pull-right"><center>8<br>GALLONS</center></div></a>
<a class="list-group-item benefit"><img src="http://192.168.0.6/resources/images/tree.png"><div class="pull-right"><center>3<br>TREES</center></div></a>
<a class="list-group-item benefit"><img src="http://192.168.0.6/resources/images/carbon.png"><div class="pull-right"><center>70<br>KG</center></div></a>
</div>
</aside>
<!-- ?? -->
<article class="col-md-9 col-md-pull-3">
<div class="panel panel-default">
<div class="panel-heading">
Home </div>
<div class="panel-body"><table class="table table-condensed table-striped table-bordered">
<tr>
<!-- ECU ID -->
<th scope="row" class="col-xs-6">ECU ID</th>
<td>204000009924</td>
</tr>
<tr>
<!-- ????? -->
<th scope="row">Lifetime generation</th>
<td>96.42 kWh </td>
</tr>
<tr>
<!-- ???????? -->
<th scope="row">Last System Power</th>
<td>419 W </td>
</tr>
<tr>
<!-- ????????? -->
<th scope="row">Generation of Current Day</th>
<td>3.19 kWh </td>
</tr>
<tr>
<!-- ??????????? -->
<th scope="row">Last Connection to website</th>
<td>2018-05-20 17:46:35</td>
</tr>
<tr>
<!-- ?????? -->
<th scope="row">Number of Inverters</th>
<td>2</td>
</tr>
<tr>
<!-- ??????????? -->
<th scope="row">Last Number of Inverters Online</th>
<td>2</td>
</tr>
<tr>
<!-- ????? -->
<th scope="row">Current Software Version</th>
<td>V4.1</td>
</tr>
<tr>
<!-- ???? -->
<th scope="row">Current Time Zone</th>
<td>Europe/Paris</td>
</tr>
<tr>
<!-- ECU????Mac?? -->
<th scope="row">ECU Eth0 Mac Address</th>
<td>80:97:1B:00:00:00</td>
</tr>
<tr>
<!-- ECU????Mac?? -->
<th scope="row">ECU Wlan0 Mac Address</th>
<td>60:C5:A8:aa:aa:aa</td>
</tr>
<tr>
<!-- ????????? -->
<th scope="row">Inverter Comm. Signal Level</th>
<td>4</td>
</tr>
</table>
<script>
var weekday_en = new Array(7);
weekday_en[0]="Sunday";
weekday_en[1]="Monday";
weekday_en[2]="Tuesday";
weekday_en[3]="Wednesday";
weekday_en[4]="Thursday";
weekday_en[5]="Friday";
weekday_en[6]="Saturday";
var weekday_ch = new Array(7);
weekday_ch[0]="???";
weekday_ch[1]="???";
weekday_ch[2]="???";
weekday_ch[3]="???";
weekday_ch[4]="???";
weekday_ch[5]="???";
weekday_ch[6]="???";
var today = new Date();
today.setFullYear(2018);
today.setMonth(5-1);
today.setDate(20);
today.setHours(17);
today.setMinutes(49);
today.setSeconds(50);
var timestamp = today.getTime();
/* ?????? */
runTime();
function runTime() {
setTimeout(runTime, 1000);
timestamp = timestamp + 1000;
today.setTime(timestamp);
showTime();
}
function showTime() {
var year, month, date, hour, minute, second, day;
year = today.getFullYear();
month = today.getMonth() + 1;
date = today.getDate();
hour = today.getHours();
minute = today.getMinutes();
second = today.getSeconds();
day = today.getDay();
if (month < 10) { month = "0" + month;}
if (date < 10) { date = "0" + date;}
if (hour < 10) { hour = "0" + hour;}
if (minute < 10) { minute = "0" + minute;}
if (second < 10) { second = "0" + second;}
if (/[\u4E00-\u9FA5]/i.test($("#ecu_title").html())) {
$('#ecu_time').html(year + "?" + month + "?" + date + "? " + hour + ":" + minute + ":" + second + "<br>" + weekday_ch[day]);
}
else {
$('#ecu_time').html(year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second + "<br>" + weekday_en[day]);
}
}
</script>
</div>
</div>
</article>
</div>
</section>
<footer class="footer">© 2015 Altenergy Power System Inc.</footer>
<script>
/* ??5?????? */
function myrefresh() {
window.location.reload();
}
setTimeout('myrefresh()',300000);
/* ???? */
$(".chlang").click(function(){
$.ajax({
url : "http://192.168.0.6/index.php/management/set_language",
type : "post",
dataType : "json",
data: "language=" + $(this).attr("id"),
})
setTimeout("location.reload();",500);//????
});
</script>
</body>
</html>
Re: récupéer des données sur une page WEB simplement
Publié : 20 mai 2018, 18:08
par marcus_54
spiwit, j'ai mis ton script dans /domoticz/scripts
Code : Tout sélectionner
#/bin/bash
url="http://192.168.0.6/index.php/home"
valeur=`curl -s $url | grep -A1 '<th scope="row">Last System Power</th>' | tail -n 1 | awk -F " " '{print $1}' | sed s/'<td>'//g`
echo $valeur
à l’exécution (./ecu.sh) sous ssh je n'ai pas de résultat visible

Re: récupéer des données sur une page WEB simplement
Publié : 20 mai 2018, 21:53
par papoo
Code : Tout sélectionner
--[[
name : script_time_AltenergyPowerControl.lua
auteur : papoo
Mise à jour : 20/05/2018
date : 20/05/2018
Principe :
20/05/2018
]]--
--------------------------------------------
------------ Variables à éditer ------------
--------------------------------------------
local debugging = true -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
local script_actif = true -- active (true) ou désactive (false) ce script simplement
local delai = 1 -- délai d'exécution de ce script en minutes de 1 à 59
local adresse = "http://192.168.1.25/test3.html"
--------------------------------------------
----------- Fin variables à éditer ---------
--------------------------------------------
local nom_script = 'Altenergy Power Control'
local version = '0.1'
--------------------------------------------
---------------- Fonctions -----------------
--------------------------------------------
function voir_les_logs (s, debugging) -- nécessite la variable local debugging
if (debugging) then
if s ~= nil then
print (s)
else
print ("aucune valeur affichable")
end
end
end -- usage voir_les_logs("=========== ".. nom_script .." (v".. version ..") ===========",debugging)
--------------------------------------------
-------------- Fin Fonctions ---------------
--------------------------------------------
commandArray = {}
time = os.date("*t")
if script_actif == true then
--if (time.hour == 00 and time.min == 20) then -- tout les matins à 00h20
--if (time.min == 20 and ((time.hour == 7) or (time.hour == 13) or (time.hour == 18))) then -- 3 exécutions du script par jour à 7H20, 13h20 et 18H20
-- if time.hour % 2 == 0 then -- toutes les deux heures
-- if (time.hour%1 == 0 and time.min == 10) then -- Toutes les heures et 10 minutes
-- if time.min% 2 == 0 then
if ((time.min-1) % delai) == 0 then -- toutes les xx minutes en commençant par xx:01. xx définissable via la variable delai
voir_les_logs("=========== ".. nom_script .." (v".. version ..") ===========",debugging)
if adresse ~= '' then voir_les_logs('--- --- --- Adresse : '..adresse..' --- --- --',debugging) end
local rid = assert(io.popen("/usr/bin/curl -m5 ".. adresse))
local testhtml = rid:read('*all')
rid:close()
for instance in testhtml:gmatch("<body>(.-)</body>") do
div, LifetimeGeneration=instance:match('Lifetime generation(.-)<td>(.-)kWh </td>')
div1, LastSystemPower=instance:match('Last System Power(.-)<td>(.-)W </td>')
div2, GenerationofCurrentDay=instance:match('Generation of Current Day(.-)<td>(.-)kWh </td>')
end
if LifetimeGeneration ~= nil then
voir_les_logs("--- --- --- Lifetime Generation : ".. LifetimeGeneration.."kWh --- --- --",debugging)
else
voir_les_logs('--- --- --- Lifetime Generation est vide --- --- --',debugging)
end
if LastSystemPower ~= nil then
voir_les_logs("--- --- --- Last System Power : ".. LastSystemPower.."W --- --- --",debugging)
else
voir_les_logs('--- --- --- LastSystemPower est vide --- --- --',debugging)
end
if GenerationofCurrentDay ~= nil then
voir_les_logs("--- --- --- Generation of Current Day : ".. GenerationofCurrentDay.."kWh --- --- --",debugging)
else
voir_les_logs('--- --- --- GenerationofCurrentDay est vide --- --- --',debugging)
end
voir_les_logs("========= Fin ".. nom_script .." (v".. version ..") =========",debugging)
end -- if time
end
return commandArray
Re: récupéer des données sur une page WEB simplement
Publié : 21 mai 2018, 09:14
par marcus_54
Un grand merci papoo et bravo !
je l'ai mis avec "Evénements" "lua" "time"
La lecture des valeurs fonctionne dans les log
Comment je récupère les valeurs ? y a-t-il un "dummy" qui va bien ?
j'ai ajouté "local ECU1_device = 720 "
et j'ai mis un "compteur électrique" mais il y a rien
Code : Tout sélectionner
2018-05-21 09:12:03.346 dzVents: =========== Altenergy Power Control (v0.1) ===========
2018-05-21 09:12:03.346 dzVents: --- --- --- Adresse : http://192.168.0.6/index.php/home --- --- --
2018-05-21 09:12:03.697 dzVents: --- --- --- Lifetime Generation : 97.40 kWh --- --- --
2018-05-21 09:12:03.697 dzVents: --- --- --- Last System Power : 431 W --- --- --
2018-05-21 09:12:03.697 dzVents: --- --- --- Generation of Current Day : 0.46 kWh --- --- --
2018-05-21 09:12:03.697 dzVents: ========= Fin Altenergy Power Control (v0.1) =========
Re: récupéer des données sur une page WEB simplement
Publié : 21 mai 2018, 09:17
par papoo
Effectivement il n’y a que la remontée d’info dans les logs
Pour tester le fonctionnement
Maintenant, en t’inspirant des scripts de cette section, tu va vite trouver comment faire un update device
Re: récupéer des données sur une page WEB simplement
Publié : 21 mai 2018, 09:29
par marcus_54
Ça marche
je te tiens au courant dans la journée
Encore merci
Re: récupéer des données sur une page WEB simplement
Publié : 21 mai 2018, 12:29
par marcus_54
Ca commence à fonctionner
721 Solaire 82721 1 Solaire production P1 Smart Meter Energy 99.55 ;0;0;0;569 ;0
j'ai pris "P1 compteur intelligent." Par contre je n'ai pas trouvé plus de détail sur les champs qu'il utilise.
• USAGE 1 : compteur HC,
• USAGE 2 : compteur HP,
• RETURN 1 : 0,
• RETURN 2 : 0,
• CONS : compteur puissance apparente,
• PROD : 0
Code : Tout sélectionner
--[[
name : script_time_AltenergyPowerControl.lua
auteur : papoo
Mise à jour : 20/05/2018
date : 20/05/2018
Principe :
20/05/2018
]]--
--------------------------------------------
------------ Variables à éditer ------------
--------------------------------------------
local nom_script = "ECUSolaire"
local debugging = true -- true pour voir les logs dans la console log Dz ou false pour ne pas les voir
local script_actif = true -- active (true) ou désactive (false) ce script simplement
local delai = 1 -- délai d'exécution de ce script en minutes de 1 à 59
local ECU_device = 721 -- renseigner l'id du device texte associé (dummy - text) P1 compteur intelligent
local adresse = "http://192.168.0.6/index.php/home"
--------------------------------------------
----------- Fin variables à éditer ---------
--------------------------------------------
local nom_script = 'Altenergy Power Control'
local version = '0.3'
--------------------------------------------
---------------- Fonctions -----------------
--------------------------------------------
function voir_les_logs (s, debugging) -- nécessite la variable local debugging
if (debugging) then
if s ~= nil then
print (s)
else
print ("aucune valeur affichable")
end
end
end -- usage voir_les_logs("=========== ".. nom_script .." (v".. version ..") ===========",debugging)
--------------------------------------------
-------------- Fin Fonctions ---------------
--------------------------------------------
commandArray = {}
time = os.date("*t")
if script_actif == true then
--if (time.hour == 00 and time.min == 20) then -- tout les matins à 00h20
--if (time.min == 20 and ((time.hour == 7) or (time.hour == 13) or (time.hour == 18))) then -- 3 exécutions du script par jour à 7H20, 13h20 et 18H20
--if time.hour % 2 == 0 then -- toutes les deux heures
--if (time.hour%1 == 0 and time.min == 10) then -- Toutes les heures et 10 minutes
--if time.min% 2 == 0 then
if ((time.min-1) % delai) == 0 then -- toutes les xx minutes en commençant par xx:01. xx définissable via la variable delai
voir_les_logs("=========== ".. nom_script .." (v".. version ..") ===========",debugging)
if adresse ~= '' then voir_les_logs('--- --- --- Adresse : '..adresse..' --- --- --',debugging) end
local rid = assert(io.popen("/usr/bin/curl -m5 ".. adresse))
local testhtml = rid:read('*all')
rid:close()
for instance in testhtml:gmatch("<body>(.-)</body>") do
div, LifetimeGeneration=instance:match('Lifetime generation(.-)<td>(.-)kWh </td>')
div1, LastSystemPower=instance:match('Last System Power(.-)<td>(.-)W </td>')
div2, GenerationofCurrentDay=instance:match('Generation of Current Day(.-)<td>(.-)kWh </td>')
end
if LifetimeGeneration ~= nil then
voir_les_logs("--- --- --- Lifetime Generation : ".. LifetimeGeneration.."kWh --- --- --",debugging)
else
voir_les_logs('--- --- --- Lifetime Generation est vide --- --- --',debugging)
end
if LastSystemPower ~= nil then
voir_les_logs("--- --- --- Last System Power : ".. LastSystemPower.."W --- --- --",debugging)
else
voir_les_logs('--- --- --- LastSystemPower est vide --- --- --',debugging)
end
if GenerationofCurrentDay ~= nil then
voir_les_logs("--- --- --- Generation of Current Day : ".. GenerationofCurrentDay.."kWh --- --- --",debugging)
else
voir_les_logs('--- --- --- GenerationofCurrentDay est vide --- --- --',debugging)
end
commandArray['UpdateDevice'] = ECU_device.."|0|"..LifetimeGeneration..";0;0;0;"..LastSystemPower..";0"
voir_les_logs("========= Fin ".. nom_script .." (v".. version ..") =========",debugging)
end -- if time
end
return commandArray