理解LUA的urlGet和RegEx

在项目中使用 urlGet 并缺乏经验,没有找到一个带有示例的好资源来自己学,所以希望这里的社区能够帮助我。

我正在尝试从一个网页中提取一些数据,具体来说是:

https://rrtp.comed.com/rrtp/ServletFeed?type=instanthourly

在这个页面上是实时定价的数据,针对在一个叫做住宅实时定价的计划上的人们。我想从这个页面中提取数字成本数据,用于家庭自动化系统。

我正在使用另一个类似的驱动程序作为我的工作基础示例,但它不是一个直接的转换,因为该示例是通过 Yahoo 天气 API 获取数据。但是,从我所了解的情况来看,基本原理应该是相同的。这是示例代码的相关片段,具体来说是查看 RequestData 函数和 ParseData 部分:

function Init()
    -- Create Variables
    for k, v in pairs(weatherData) do
        if (not Variables[k]) then
            C4:AddVariable(k, v, "NUMBER", true, false)
        end
    end
end

function RequestData()
    local query = "select%20wind%2C%20atmosphere%2C%20item.title%2C%20item.condition.code%2C%20item.condition.temp%20from%20weather.forecast%20where%20woeid%3D" .. Properties[WOEID] .. "%20and%20u%3D%22" .. Properties[UNIT] .. "%22"
    C4:urlGet(string.format("http://query.yahooapis.com/v1/public/yql?q=%s", query))
end

function ReceivedAsync(ticketId, strData, responseCode, tHeaders)
    dbg(string.format("ReceivedAsync[%s]: %s", ticketId, strData))

    for k, v in pairs(weatherData) do
        local tempVal = ParseData(strData, k)

        -- Set property, table, and variable
        Properties[displayProperties[k]] = tempVal
        weatherData[k] = tempVal
        C4:SetVariable(k, tostring(tempVal))

        OnPropertyChanged(displayProperties[k])

        if (tempVal ~= weatherData[k]) then
            if (k == RISING) then
                tempVal = weatherRising[tonumber(tempVal)] or "N/A"
            end
        end

        C4:UpdateProperty(displayProperties[k], tostring(tempVal))
    end

    if(weatherData["code"] ~= "N/A" and weatherData["code"] ~= nil) then
        weatherData["condition"] = weatherConditions[tonumber(weatherData["code"])]
        C4:UpdateProperty(CONDITION, weatherData["condition"])
    end

    for k, v in pairs(weatherData) do
        dbg(displayProperties[k] .. ":" .. v)
    end
end

function ParseData(strData, item)
    return string.match(strData, string.format("%s=\"(.-)\"", item)) or
    string.match(strData, string.format("<%s>Conditions for (.+)</%s>", item, item)) or
    "N/A"
end

所以……

我首先要问的问题,我认为我知道答案,就是当你使用 urlGet 时,它是如何看待网页的?即它是否看到了原始的 HTML 或没有所有的 HTML 标记。

我问这个问题是因为我相信它会改变我需要构建的正则表达式,以提取数字数据。

这是我提议的 urlGet 和 string.match。如果我朝着正确的方向前进,我会很感激任何见解,也希望得到任何其他的建议。

function RequestData()
    C4:urlGet(string.format("https://rrtp.comed.com/rrtp/ServletFeed type=instanthourly))
end

.

function ParseData(strData, item)
    return string.match(strData, string.format("\b([0-9]\.[0-9])\b")) or
    "N/A"
end
点赞
用户107090
用户107090

尝试使用此模式提取价格: "(%d+%.%d+)"。请注意,Lua模式中的转义符为

2014-03-02 12:35:19