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

 找回密码
 立即注册
查看: 1392|回复: 8

[流程系列] 【nodered】获取苏州地铁到站时间

[复制链接]

5

主题

44

帖子

316

积分

中级会员

Rank: 3Rank: 3

积分
316
金钱
272
HASS币
0
发表于 2024-2-26 17:58:49 | 显示全部楼层 |阅读模式

偶然间发现苏州地铁有个查询还有多久到站的页面就顺手写了个。

配合微信出门推送, 以后老婆就不用每天出门火急火燎的了(这里没包含微信推送)

IMG_0681.PNG

我这里用了8个html解析,其实可以用一个搞定的,主要是发现的时候已经写完了懒得改了。。


需要提前在全局变量中设置 cheerio <-> require('cheerio')
在setting.js中引入cheerio相关依赖
[
    {
        "id": "aaaee5847ee47cc2",
        "type": "http request",
        "z": "56723380.1b6ddc",
        "name": "",
        "method": "GET",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://106.14.198.252:8512",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 600,
        "y": 800,
        "wires": [
            [
                "96af9a1702ddba60",
                "5223ac346e5f7ea4",
                "44ba7cb3d730ae71",
                "2cd915cc1a4897dd",
                "1aae5bfc3267a744",
                "9ac1ac60c8f3627f",
                "d1e328bca68b19e2",
                "ae38606b4ac1fd72"
            ]
        ]
    },
    {
        "id": "96af9a1702ddba60",
        "type": "html",
        "z": "56723380.1b6ddc",
        "name": "当前班次-预计时间1",
        "property": "payload",
        "outproperty": "time1",
        "tag": "#app > div.container > div > div:nth-child(4) > div.staTre-timeline-tableBody > div.tableLeft > div.inStation-time",
        "ret": "text",
        "as": "single",
        "x": 820,
        "y": 680,
        "wires": [
            [
                "8079ff04d8fc26d8"
            ]
        ]
    },
    {
        "id": "5223ac346e5f7ea4",
        "type": "html",
        "z": "56723380.1b6ddc",
        "name": "当前班次-行进方向1",
        "property": "payload",
        "outproperty": "fangxiang1",
        "tag": "#app > div.container > div > div:nth-child(4) > div.staTre-timeline-tableHead > div.tableLeft",
        "ret": "text",
        "as": "single",
        "x": 820,
        "y": 640,
        "wires": [
            [
                "8079ff04d8fc26d8"
            ]
        ]
    },
    {
        "id": "8079ff04d8fc26d8",
        "type": "join",
        "z": "56723380.1b6ddc",
        "name": "",
        "mode": "custom",
        "build": "merged",
        "property": "",
        "propertyType": "full",
        "key": "topic",
        "joiner": "\\n",
        "joinerType": "str",
        "accumulate": false,
        "timeout": "1",
        "count": "",
        "reduceRight": false,
        "reduceExp": "",
        "reduceInit": "",
        "reduceInitType": "num",
        "reduceFixup": "",
        "x": 1030,
        "y": 780,
        "wires": [
            [
                "13ee4bd464fc469d"
            ]
        ]
    },
    {
        "id": "2cd915cc1a4897dd",
        "type": "html",
        "z": "56723380.1b6ddc",
        "name": "当前班次-预计时间2",
        "property": "payload",
        "outproperty": "time2",
        "tag": "#app > div.container > div > div:nth-child(4) > div.staTre-timeline-tableBody > div.tableRight > div.inStation-time",
        "ret": "text",
        "as": "single",
        "x": 820,
        "y": 760,
        "wires": [
            [
                "8079ff04d8fc26d8"
            ]
        ]
    },
    {
        "id": "44ba7cb3d730ae71",
        "type": "html",
        "z": "56723380.1b6ddc",
        "name": "当前班次-行进方向2",
        "property": "payload",
        "outproperty": "fangxiang2",
        "tag": "#app > div.container > div > div:nth-child(4) > div.staTre-timeline-tableHead > div.tableRight",
        "ret": "text",
        "as": "single",
        "x": 820,
        "y": 720,
        "wires": [
            [
                "8079ff04d8fc26d8"
            ]
        ]
    },
    {
        "id": "9ac1ac60c8f3627f",
        "type": "html",
        "z": "56723380.1b6ddc",
        "name": "下一班次-预计时间1",
        "property": "payload",
        "outproperty": "next_time1",
        "tag": "#app > div.container > div > div:nth-child(7) > div.staTre-timeline-tableBody > div.tableLeft > div.inStation-time",
        "ret": "text",
        "as": "single",
        "x": 820,
        "y": 860,
        "wires": [
            [
                "8079ff04d8fc26d8"
            ]
        ]
    },
    {
        "id": "1aae5bfc3267a744",
        "type": "html",
        "z": "56723380.1b6ddc",
        "name": "下一班次-行进方向1",
        "property": "payload",
        "outproperty": "next_fangxiang1",
        "tag": "#app > div.container > div > div:nth-child(7) > div.staTre-timeline-tableHead > div.tableLeft",
        "ret": "text",
        "as": "single",
        "x": 820,
        "y": 820,
        "wires": [
            [
                "8079ff04d8fc26d8"
            ]
        ]
    },
    {
        "id": "ae38606b4ac1fd72",
        "type": "html",
        "z": "56723380.1b6ddc",
        "name": "下一班次-预计时间2",
        "property": "payload",
        "outproperty": "next_time2",
        "tag": "#app > div.container > div > div:nth-child(7) > div.staTre-timeline-tableBody > div.tableRight > div.inStation-time",
        "ret": "text",
        "as": "single",
        "x": 820,
        "y": 940,
        "wires": [
            [
                "8079ff04d8fc26d8"
            ]
        ]
    },
    {
        "id": "d1e328bca68b19e2",
        "type": "html",
        "z": "56723380.1b6ddc",
        "name": "下一班次-行进方向2",
        "property": "payload",
        "outproperty": "next_fangxiang2",
        "tag": "#app > div.container > div > div:nth-child(7) > div.staTre-timeline-tableHead > div.tableRight",
        "ret": "text",
        "as": "single",
        "x": 820,
        "y": 900,
        "wires": [
            [
                "8079ff04d8fc26d8"
            ]
        ]
    },
    {
        "id": "281f34019a2eb718",
        "type": "http request",
        "z": "56723380.1b6ddc",
        "name": "请求站点信息",
        "method": "GET",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "http://106.14.198.252:8512/station",
        "tls": "",
        "persist": false,
        "proxy": "",
        "insecureHTTPParser": false,
        "authType": "",
        "senderr": false,
        "headers": [],
        "x": 480,
        "y": 720,
        "wires": [
            [
                "dc912886ba410687"
            ]
        ]
    },
    {
        "id": "b5dfc9ce023a7d1c",
        "type": "inject",
        "z": "56723380.1b6ddc",
        "name": "每天早上8:11",
        "props": [
            {
                "p": "querySite",
                "v": "索山桥西",
                "vt": "str"
            },
            {
                "p": "toUser",
                "v": "zouxiu",
                "vt": "str"
            }
        ],
        "repeat": "",
        "crontab": "50 07 * * *",
        "once": false,
        "onceDelay": 0.1,
        "topic": "",
        "x": 480,
        "y": 640,
        "wires": [
            [
                "281f34019a2eb718"
            ]
        ]
    },
    {
        "id": "dc912886ba410687",
        "type": "function",
        "z": "56723380.1b6ddc",
        "name": "站点模糊匹配",
        "func": "const cheerio = global.get('cheerio');\n\n\n// 使用 cheerio 加载 HTML\nconst $ = cheerio.load(msg.payload);\n\n\n\n// 最终的 JSON 结构\nconst sites = [];\n\n// 遍历每个 .right-con 元素\n$('.right-con').each((index, div) => {\n    const stationNames = []; // 存储当前 div 下的所有站名\n\n    // 遍历当前 div 下的所有 h4 元素\n    $(div).find('h4').each((idx, h4) => {\n        // 获取 h4 元素中的文本并添加到列表中\n        stationNames.push($(h4).text());\n    });\n\n    // 如果当前 div 下有站名,则将列表添加到结果中\n    if (stationNames.length > 0) {\n        sites.push(stationNames);\n    }\n});\n\n\n// 创建一个空对象存储 v-line 和文本值\nconst lines = [];\n\n// 遍历所有的 li 元素\n$('body > div > div.left-menu > ul > li').each((index, element) => {\n  // 获取当前元素的 v-line 属性\n  const vLine = $(element).attr('v-line');\n  // 获取当前元素的文本内容,同时去除所有的空白字符(包括空格和换行符)\n  const value = $(element).clone().children().remove().end().text().trim();\n    lines.push({\n    "lineId": vLine,\n    "lineName": value\n  })\n});\n\nlet queryLine = {}\nlet queryStation = "";\nlet flag = false;\nfor(let i = 0; i < sites.length;i++) {\n    for(let j = 0; j < sites[i].length;j++) {\n        if (sites[i][j].includes(msg.querySite)) {\n            queryLine = lines[i];\n            queryStation= sites[i][j];\n            flag = true\n            break;\n        }\n    }\n    if(flag) {\n        break;\n    }\n}\n\nlet encodeStation=encodeURIComponent(queryStation)\nlet today = new Date().getDay() == 0 ? 7 : new Date().getDay();\nmsg.headers = { \n    'Cookie': `weekday=${today}; line=${queryLine.lineId}; station=${encodeStation}`\n };\n\nmsg.flag = flag\n// 打印结果对象\nreturn msg;",
        "outputs": 1,
        "timeout": 0,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 480,
        "y": 760,
        "wires": [
            [
                "b766f2408c6d777b"
            ]
        ]
    },
    {
        "id": "b766f2408c6d777b",
        "type": "switch",
        "z": "56723380.1b6ddc",
        "name": "查到结果",
        "property": "flag",
        "propertyType": "msg",
        "rules": [
            {
                "t": "true"
            }
        ],
        "checkall": "true",
        "repair": false,
        "outputs": 1,
        "x": 460,
        "y": 800,
        "wires": [
            [
                "aaaee5847ee47cc2"
            ]
        ]
    }
]


评分

参与人数 1金钱 +8 收起 理由
zxyny1989 + 8 大神666!

查看全部评分

回复

使用道具 举报

40

主题

2176

帖子

8284

积分

元老级技术达人

积分
8284
金钱
6093
HASS币
110
发表于 2024-2-27 17:41:11 | 显示全部楼层
搞了个南京的
回复

使用道具 举报

0

主题

109

帖子

1313

积分

金牌会员

Rank: 6Rank: 6

积分
1313
金钱
1204
HASS币
0
发表于 2024-2-28 09:02:56 | 显示全部楼层
今天早上试了一下,不准,网页显示还有2分钟,其实车已经到了,误差很大,没有参考意义。
回复

使用道具 举报

0

主题

109

帖子

1313

积分

金牌会员

Rank: 6Rank: 6

积分
1313
金钱
1204
HASS币
0
发表于 2024-2-29 09:00:07 来自手机 | 显示全部楼层
今天又试了一下,显示地铁到站,其实还有两分钟。
回复

使用道具 举报

5

主题

44

帖子

316

积分

中级会员

Rank: 3Rank: 3

积分
316
金钱
272
HASS币
0
 楼主| 发表于 2024-3-1 15:55:13 | 显示全部楼层
silversucks 发表于 2024-2-29 09:00
今天又试了一下,显示地铁到站,其实还有两分钟。

这个数据是苏州本地宝捞出来的, 我也不知道哪儿来的
回复

使用道具 举报

5

主题

44

帖子

316

积分

中级会员

Rank: 3Rank: 3

积分
316
金钱
272
HASS币
0
 楼主| 发表于 2024-3-6 17:44:21 | 显示全部楼层
silversucks 发表于 2024-2-29 09:00
今天又试了一下,显示地铁到站,其实还有两分钟。

昨天特意问了一下我老婆, 她说挺准的,我人不在苏州实验不了
回复

使用道具 举报

0

主题

109

帖子

1313

积分

金牌会员

Rank: 6Rank: 6

积分
1313
金钱
1204
HASS币
0
发表于 2024-3-7 09:44:06 | 显示全部楼层
bestsort 发表于 2024-3-6 17:44
昨天特意问了一下我老婆, 她说挺准的,我人不在苏州实验不了

我后来看了一下,这个网站来源是苏州轨道交通官网,但没一次是准的,上下班都观察了很多次。
回复

使用道具 举报

27

主题

228

帖子

1761

积分

金牌会员

Rank: 6Rank: 6

积分
1761
金钱
1533
HASS币
0
发表于 2024-3-7 11:39:27 | 显示全部楼层
高德有没有地铁的车次和到站时间?
回复

使用道具 举报

5

主题

44

帖子

316

积分

中级会员

Rank: 3Rank: 3

积分
316
金钱
272
HASS币
0
 楼主| 发表于 2024-3-7 14:56:50 | 显示全部楼层
huyifanstar 发表于 2024-3-7 11:39
高德有没有地铁的车次和到站时间?

地铁没有的,只有公交有
回复

使用道具 举报

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

本版积分规则

Archiver|手机版|小黑屋|Hassbian

GMT+8, 2024-11-22 05:13 , Processed in 0.266648 second(s), 35 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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