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

 找回密码
 立即注册
查看: 1486|回复: 2

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

[复制链接]

5

主题

47

帖子

827

积分

论坛技术达人

积分
827
金钱
775
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

主题

2178

帖子

8338

积分

元老级技术达人

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

使用道具 举报

2

主题

26

帖子

85

积分

注册会员

Rank: 2

积分
85
金钱
59
HASS币
0
发表于 4 天前 | 显示全部楼层

用的nodered里面的exec,提示no module

本帖最后由 audie 于 2024-12-23 18:29 编辑
Command failed: python3 /python/water_free/water.py
Traceback (most recent call last):
  File "/python/water_free/water.py", line 1, in <module>
    import requests
ModuleNotFoundError: No module named 'requests'




回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-12-27 08:40 , Processed in 0.191963 second(s), 27 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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