『瀚思彼岸』» 智能家居技术论坛

 找回密码
 立即注册
查看: 1374|回复: 1

[经验分享] 南京水费脚本-免抓cookie

[复制链接]

5

主题

47

帖子

819

积分

论坛技术达人

积分
819
金钱
767
HASS币
20
发表于 2023-11-24 10:21:09 | 显示全部楼层 |阅读模式
本帖最后由 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];

接入如图所示

接入图

接入图





回复

使用道具 举报

40

主题

2176

帖子

8286

积分

元老级技术达人

积分
8286
金钱
6095
HASS币
110
发表于 2023-11-24 13:18:51 | 显示全部楼层
我又来了 江宁不算南京 哈哈
江宁水务抓了cookie用了好久了 非常稳定
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-24 09:17 , Processed in 0.056091 second(s), 26 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表