|
本帖最后由 freefitter 于 2023-11-24 10:47 编辑
之前的水费是需要自己去抓Cookie,然后填在配置文件中,现在不需要了,使用了带带弟弟OCR通用验证码识别SDK免费开源版,来进行验证码识别,并进行模拟登陆。这边只取了水费信息,如果需要其他的可以自己修改。然后在Node-Red中执行这个脚本就可以了。
之前插件的地址 南京水费插件,也做了更新。
import requests
import random
import ddddocr
from fake_useragent import UserAgent
ua = UserAgent()
user_Agent = ua.random
headers = {
"User-Agent": user_Agent,
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Language": "zh-CN,zh;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"DNT": "1",
"Connection": "cloes"
}
ocr = ddddocr.DdddOcr()
if __name__ == '__main__':
num = random.uniform(0, 1)
# Verification code
img_src = 'https://www.jlwater.com/captcha?' + str(num)
print("Verification code url: " + img_src)
r = requests.get(img_src, headers=headers)
# save cookie
cookies = r.cookies.items()
cookie = ''
for name, value in cookies:
cookie += '{0}={1};'.format(name, value)
# save Verification code img
# with open("yzm.jpeg", "wb") as f:
# f.write(r.content)
res = ocr.classification(r.content)
print("The code is:", res)
# login
url = "https://www.jlwater.com/user/ajaxLogin"
payload = {
"mobile": "填写你的手机号",
"password": "抓包查看下加密后的",
"code": res,
"url": ""
}
headers = {
"User-Agent": user_Agent,
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9",
"Accept-Language": "zh-CN,zh;q=0.9",
"Accept-Encoding": "gzip, deflate, br",
"cookie": cookie
}
res = requests.post(url, data=payload, headers=headers)
cookies = r.cookies.items()
for name, value in cookies:
cookie += '{0}={1};'.format(name, value)
# water fee
url = "https://www.jlwater.com/waterFee/getConsWaterFeeSummary"
payload = {'consNo': '你的户号'}
response = requests.request("POST", url, headers=headers, data=payload)
print(response.text)
可以使用Node-Red直接调用 exec 节点调用。然后生成MQTT传感器。
function toDecimal2(x) {
var f = Math.round(x * 100) / 100;
var s = f.toString();
var rs = s.indexOf('.');
if (rs < 0) {
rs = s.length;
s += '.';
}
while (s.length <= rs + 2) {
s += '0';
}
return s;
}
const prefix = "home/sensor/water_fee"
let msg_config = []
msg_config = [
{
"topic": "homeassistant/sensor/water_fee/config",
"payload": {
"~": prefix.replace("button", "sensor"),
"name": "water_fee",
"state_topic": "~/state",
"unique_id": "water_fee",
"icon": "mdi:water",
"json_attributes_topic": "~/info"
}
},
{
"topic": `${prefix.replace("button", "sensor")}/state`,
"payload": toDecimal2(msg.payload.waterFee)
},
{
"topic": `${prefix.replace("button", "sensor")}/info`,
"payload": msg.payload
}
]
return [msg_config];
接入如图所示
接入图
|
|