Python之DingTalk機器人推送天氣預報

來源:互聯網
上載者:User

標籤:xxxxx   data   access   推送   windows   for   ike   gen   ima   

通過Python指令碼結合DingTalk機器人,定時向DingTalk群推送天氣預報

#!/usr/bin/python# -*- coding: utf-8 -*-# Author: [email protected]# My blog http://m51cto.51cto.blog.com import requestsimport reimport urllib2import jsonimport sysimport osheaders = {‘Content-Type‘: ‘application/json;charset=utf-8‘}api_url = "https://oapi.dingtalk.com/robot/send?access_token=37e23308d1b84eb4ac34566e03c4c4e74bxxx7xxxxxxxxxxx"##從DingTalk機器人設定中拷貝  def msg(text):    json_text= {     "msgtype": "text",        "at": {            "atMobiles": [                "132xxxx1280"            ],            "isAtAll": False        },        "text": {            "content": text        }    }    print requests.post(api_url,json.dumps(json_text),headers=headers).contenthearders = "User-Agent","Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/60.0.3112.113 Safari/537.36"url = "https://tianqi.moji.com/weather/china/guangdong/shenzhen"    ##要爬去天氣預報的網址par = ‘(<meta name="description" content=")(.*?)(">)‘    ##正則匹配,匹配出網頁內要的內容##建立opener對象並設定為全域對象opener = urllib2.build_opener()opener.addheaders = [hearders]urllib2.install_opener(opener)##擷取網頁html = urllib2.urlopen(url).read().decode("utf-8")##提取需要爬取的內容data = re.search(par,html).group(2)msg(data)

拷貝指令碼時請去掉注釋
運行指令碼:

python weather.py {"errcode":0,"errmsg":"ok"}

DingTalk群收到訊息

做計劃任務:

# crontab -eno crontab for root - using an empty one30 8 * * * /usr/bin/python /root/script/weather.py 2> /dev/null > /dev/null

每天早晨8:30自動推送天氣預報到DingTalk群

Python之DingTalk機器人推送天氣預報

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.