2023年1月10日 星期二

Pytthon 爬取臺灣銀行牌告匯率 --> Line Notify , Node-red MQTT

 Pytthon 爬取臺灣銀行牌告匯率

 源自於https://steam.oxxostudio.tw/category/python/spider/exchange.html





import requests

import paho.mqtt.client as mqtt #import the client1

import time

publish=[]

#=================================#

def on_message(client, userdata, message):

    print("message received " ,str(message.payload.decode("utf-8")))

    print("message topic=",message.topic)

    print("message qos=",message.qos)

    print("message retain flag=",message.retain)

#=================================#

broker_address="broker.mqtt-dashboard.com"

#broker_address="iot.eclipse.org"

print("creating new instance")

client = mqtt.Client("P1") #create new instance

client.on_message=on_message #attach function to callback

print("connecting to broker --> ",broker_address )

client.connect(broker_address) #connect to broke

#=================================#


url = 'https://rate.bot.com.tw/xrt/flcsv/0/day'   # 牌告匯率 CSV 網址

# https://rate.bot.com.tw/xrt/flcsv/0/day

rate = requests.get(url)   # 爬取網址內容

rate.encoding = 'utf-8'    # 調整回應訊息編碼為 utf-8,避免編碼不同造成亂碼

rt = rate.text             # 以文字模式讀取內容

rts = rt.split('\n')       # 使用「換行」將內容拆分成串列

#============MQTT===================#    

client.loop_start() #start the loop

#============Line Notify===================#    

url = 'https://notify-api.line.me/api/notify'    # LINE Notify API 網址

#token = '你的 LINE Notify 權杖'                   # 自己申請的 LINE Notify 權杖

token = 'A4wwPNh2WqB7dlfeQyyIAwtggn1kfZSI5LkkCdia1gB'

headers = {

    'Authorization': 'Bearer ' + token           # POST 使用的 headers

}



for i in rts:              # 讀取串列的每個項目

    try:                             # 使用 try 避開最後一行的空白行

        a = i.split(',')             # 每個項目用逗號拆分成子串列

        print(a[0] + ': ' + a[12])   # 取出第一個 ( 0 ) 和第十三個項目 ( 12 )

        #--------------------------------------------------------------------

        client.publish("alex9ufo/ratetbot",a[0] + ': ' + a[12])

        #--------------------------------------------------------------------          

        data = {# 發送的訊息

                    'message':'Python送出 目前台灣銀行牌告匯率 -->幣別  現金 '+ a[0] + ': ' + a[12]

                    }

        data = requests.post(url, headers=headers, data=data)    # 發送 LINE NOtify

        

        time.sleep(5) # wait

        #--------------------------------------------------------------------        

    except:

        break

#============MQTT===================#    


client.loop_stop() #stop the loop



#============Line Notify===================#    




>>> %Run Rate_2.py
creating new instance
connecting to broker -->  broker.mqtt-dashboard.com
幣別: 現金
USD: 30.70000
HKD: 3.94300
GBP: 37.90000
AUD: 21.36000
CAD: 23.06000
SGD: 23.17000
CHF: 33.35000
JPY: 0.23340
ZAR: 0.00000
SEK: 3.05000
NZD: 19.76000
THB: 0.96850
PHP: 0.61960
IDR: 0.00233
EUR: 33.18000
KRW: 0.02652
VND: 0.00148
MYR: 7.43400
CNY: 4.54300
>>> 








[{"id":"d3c84f39d1691d3c","type":"debug","z":"323770202b3d133d","name":"debug 58","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":480,"y":500,"wires":[]},{"id":"cc243efbb1f863bb","type":"mqtt in","z":"323770202b3d133d","name":"","topic":"alex9ufo/ratetbot","qos":"2","datatype":"auto-detect","broker":"841df58d.ee5e98","nl":false,"rap":true,"rh":0,"inputs":0,"x":240,"y":500,"wires":[["d3c84f39d1691d3c","d498ad7ef323948c"]]},{"id":"d498ad7ef323948c","type":"ui_text","z":"323770202b3d133d","group":"a73c0688.26bac8","order":1,"width":0,"height":0,"name":"","label":"牌告匯率","format":"{{msg.payload}}","layout":"row-left","className":"","x":480,"y":540,"wires":[]},{"id":"841df58d.ee5e98","type":"mqtt-broker","name":"","broker":"broker.mqtt-dashboard.com","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"15","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"a73c0688.26bac8","type":"ui_group","name":"Bits","tab":"207e613d.22c52e","order":4,"disp":false,"width":10,"collapse":false},{"id":"207e613d.22c52e","type":"ui_tab","name":"Configuration","icon":"dashboard","order":5,"disabled":false,"hidden":false}]

沒有留言:

張貼留言

Messaging API作為替代方案

  LINE超好用功能要沒了!LINE Notify明年3月底終止服務,有什麼替代方案? LINE Notify將於2025年3月31日結束服務,官方建議改用Messaging API作為替代方案。 //CHANNEL_ACCESS_TOKEN = 'Messaging ...