![]() |
|
Get temperature data from web page - Printable Version +- LogicMachine Forum (https://forum.logicmachine.net) +-- Forum: LogicMachine eco-system (https://forum.logicmachine.net/forumdisplay.php?fid=1) +--- Forum: Scripting (https://forum.logicmachine.net/forumdisplay.php?fid=8) +--- Thread: Get temperature data from web page (/showthread.php?tid=4700) |
Get temperature data from web page - Ohmarn - 04.04.2023 Hi, I want to get a temperature from a web page (Sea and water temperature) and send it to a group address. The temperature i want is "Vatten" for "Yngsjö havsbad". The closest i could find in the forum was this script, post #5, Forum post. But i get stuck when it comes to "find matching string and fetch the data you want with". Does anyone have any ideas how this could be done? Any tips or pointing me in a better direction? Best regards //Simon RE: Get temperature data from web page - Joep - 04.04.2023 require('ssl.https') data = ssl.https.request('https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html') log(data) RE: Get temperature data from web page - admin - 05.04.2023 Try this: Code: url = 'https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html'
data = require('socket.http').request(url)
lines = data:split('\n')
for _, line in ipairs(lines) do
if line:find('<tr><td headers="head11">') then
location = line:match('<tr><td headers="head11">([^,]+)')
watertemp = tonumber(line:match('<td headers="head13">([^ ]+).+</td>'))
airtemp = tonumber(line:match('<td headers="head14">([^ ]+).+</td>'))
location = location:gsub('­', '')
log(location, watertemp, airtemp)
end
endRE: Get temperature data from web page - Ohmarn - 11.04.2023 Great, but how do i split up the log to send the value i want? I tried this code, it sends a value but not corectly. It first sends the value 5,6 310 times, then 13 other values once. At least all the values are from the correct table. Code: url = 'https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html'
data = require('socket.http').request(url)
lines = data:split('\n')
for _, line in ipairs(lines) do
if line:find('<tr><td headers="head11">') then
location = line:match('<tr><td headers="head11">([^,]+)')
watertemp = tonumber(line:match('<td headers="head13">([^ ]+).+</td>'))
airtemp = tonumber(line:match('<td headers="head14">([^ ]+).+</td>'))
location = location:gsub('­', '')
log(location, watertemp, airtemp)
end
badtemp = watertemp
grp.write('10/5/101', badtemp)
endRE: Get temperature data from web page - admin - 11.04.2023 Which values exactly do you want and in which group addresses? RE: Get temperature data from web page - Ohmarn - 11.04.2023 (11.04.2023, 14:02)admin Wrote: Which values exactly do you want and in which group addresses? I want the first value (5 ℃) from this row: Yngsjö havsbad, Kristianstad 5 ℃ 8.1 ℃ (see attached pictures) Group adress: 10/5/101 4.9 ℃ 6.5 ℃ RE: Get temperature data from web page - admin - 12.04.2023 Try this: Code: url = 'https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html'
data = require('socket.http').request(url)
pos = data:find('Yngsjö havsbad')
watertemp = tonumber(data:match('<td headers="head13">([^ ]+).+</td>', pos))
grp.checkwrite('10/5/101', watertemp)RE: Get temperature data from web page - Ohmarn - 12.04.2023 Thanks, works perfectly! //Simon RE: Get temperature data from web page - Ohmarn - 23.08.2023 Hi, Since you kindly helped me last time, they've change the website and now i have to update the script.. Is anyone willing to help me, again? Source URL Value marked with blue. Best regards //Simon RE: Get temperature data from web page - fleeceable - 23.08.2023 (23.08.2023, 13:49)Ohmarn Wrote: Hi, Used ChatGTP for this: Code: url = 'https://www.havochvatten.se/badplatser-och-badvatten/vattenprov-badtemperatur/vattentemperatur-och-kvalitet-pa-badvatten-pa-sydkusten.html'
data = require('socket.http').request(url)
-- Function to extract the temperature for a specific location
local function extractTemperatureForLocation(html, locationName)
local locationStartIdx, locationEndIdx = html:find(locationName)
if not locationStartIdx then
return nil
end
local temperatureStartIdx, temperatureEndIdx, temperature = html:find("(%d+%.%d+)°C", locationEndIdx)
if not temperatureStartIdx then
return nil
end
return temperature
end
-- Location name to search for
local locationName = "Yngsjö havsbad"
-- Extract temperature for the specific location
local temperature = extractTemperatureForLocation(data, locationName)
if temperature then
-- Print the extracted temperature
--log(locationName .. " temperature: " .. temperature .. "°C")
grp.checkwrite('10/5/101', temperature)
else
log(locationName .. " temperature not found.")
end |