您好,登錄后才能下訂單哦!
這篇文章主要介紹怎么利用Python將每日一句定時(shí)推送至微信,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!
1、簡(jiǎn)單易用,與C/C++、Java、C# 等傳統(tǒng)語(yǔ)言相比,Python對(duì)代碼格式的要求沒(méi)有那么嚴(yán)格;2、Python屬于開(kāi)源的,所有人都可以看到源代碼,并且可以被移植在許多平臺(tái)上使用;3、Python面向?qū)ο?,能夠支持面向過(guò)程編程,也支持面向?qū)ο缶幊蹋?、Python是一種解釋性語(yǔ)言,Python寫(xiě)的程序不需要編譯成二進(jìn)制代碼,可以直接從源代碼運(yùn)行程序;5、Python功能強(qiáng)大,擁有的模塊眾多,基本能夠?qū)崿F(xiàn)所有的常見(jiàn)功能。
實(shí)驗(yàn)環(huán)境
Linux服務(wù)器
Python環(huán)境
調(diào)用地址:http://open.iciba.com/dsapi/
請(qǐng)求方式:GET
請(qǐng)求參數(shù):
參數(shù) | 必選 | 類(lèi)型 | 說(shuō)明 |
---|---|---|---|
date | 否 | string | 格式為:2013-05-06;如果date為,則默認(rèn)取當(dāng)天 |
type | 否 | string | 可選值為last和next;以date日期為準(zhǔn)的,last返回前一天的,next返回后一天的 |
返回類(lèi)型:JSON
JSON字段解釋?zhuān)?/p>
屬性名 | 屬性值類(lèi)型 | 說(shuō)明 |
---|---|---|
sid | string | 每日一句ID |
tts | string | 音頻地址 |
content | string | 英文內(nèi)容 |
note | string | 中文內(nèi)容 |
love | string | 每日一句喜歡個(gè)數(shù) |
translation | string | 詞霸小編 |
picture | string | 圖片地址 |
picture2 | string | 大圖片地址 |
caption | string | 標(biāo)題 |
dateline | string | 時(shí)間 |
s_pv | string | 瀏覽數(shù) |
sp_pv | string | 語(yǔ)音評(píng)測(cè)瀏覽數(shù) |
tags | array | 相關(guān)標(biāo)簽 |
fenxiang_img | string | 合成圖片,建議分享微博用的 |
正常返回示例:
{ "sid": "3080", "tts": "http://news.iciba.com/admin/tts/2018-08-01-day.mp3", "content": "No matter how hard we try to be mature, we will always be a kid when we all get hurt and cry. ", "note": "不管多努力蛻變成熟,一旦受傷哭泣時(shí),我們還是像個(gè)孩子。", "love": "1966", "translation": "小編的話(huà):這句話(huà)出自小說(shuō)《彼得·潘》。歲月永遠(yuǎn)年輕,我們慢慢老去。不管你如何蛻變,最后你會(huì)發(fā)現(xiàn):童心未泯,是一件值得驕傲的事情。長(zhǎng)大有時(shí)很簡(jiǎn)單,但凡事都能抱著一顆童心去快樂(lè)享受卻未必容易。", "picture": "http://cdn.iciba.com/news/word/20180801.jpg", "picture2": "http://cdn.iciba.com/news/word/big_20180801b.jpg", "caption": "詞霸每日一句", "dateline": "2018-08-01", "s_pv": "0", "sp_pv": "0", "tags": [ { "id": null, "name": null } ], "fenxiang_img": "http://cdn.iciba.com/web/news/longweibo/imag/2018-08-01.jpg" }
請(qǐng)求示例:
Python2請(qǐng)求示例
#!/usr/bin/python2 #coding=utf-8 import json import urllib2 def get_iciba_everyday(): url = 'http://open.iciba.com/dsapi/' request = urllib2.Request(url) response = urllib2.urlopen(request) json_data = response.read() data = json.loads(json_data) return data print get_iciba_everybody()
Python3請(qǐng)求示例
#!/usr/bin/python3 #coding=utf-8 import json import requests def get_iciba_everyday(): url = 'http://open.iciba.com/dsapi/' r = requests.get(url) return json.loads(r.text) print(get_iciba_everyday())
PHP請(qǐng)求示例
<?php function https_request($url, $data = null){ $curl = curl_init(); curl_setopt($curl, CURLOPT_URL, $url); curl_setopt($curl, CURLOPT_HEADER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); if (!empty($data)) { curl_setopt($curl, CURLOPT_POST, 1); curl_setopt($curl, CURLOPT_POSTFIELDS, $data); } curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); $output = curl_exec($curl); curl_close($curl); return $output; } function get_iciba_everyday(){ $url = 'http://open.iciba.com/dsapi/' $result = https_request($url); $data = json_decode($result); return $data; } echo get_iciba_everyday();
本接口(每日一句)官方文檔:http://open.iciba.com/?c=wiki
參考資料:金山詞霸 · 開(kāi)發(fā)平臺(tái)
登錄微信公眾平臺(tái)接口測(cè)試賬號(hào)
掃描登錄公眾平臺(tái)測(cè)試號(hào)
申請(qǐng)測(cè)試號(hào)的地址 https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
手機(jī)上確認(rèn)登錄
找到新增測(cè)試模板,添加模板消息
填寫(xiě)模板標(biāo)題每日一句,填寫(xiě)如下模板內(nèi)容
{{content.DATA}}
{{note.DATA}}
{{translation.DATA}}
提交保存之后,記住該模板ID,一會(huì)兒會(huì)用到
找到測(cè)試號(hào)信息,記住appid和appsecret,一會(huì)兒會(huì)用到
找到測(cè)試號(hào)二維碼。手機(jī)掃描此二維碼,關(guān)注之后,你的昵稱(chēng)會(huì)出現(xiàn)在右側(cè)列表里,記住該微信號(hào),一會(huì)兒會(huì)用到(注:此微信號(hào)非你真實(shí)的微信號(hào))
發(fā)送微信模板消息的程序
本程序您只需要修改4個(gè)地方即可,請(qǐng)看注釋
Python2實(shí)現(xiàn)
#!/usr/bin/python2 #coding=utf-8 import json import urllib2 class iciba: # 初始化 def __init__(self, wechat_config): self.appid = wechat_config['appid'] self.appsecret = wechat_config['appsecret'] self.template_id = wechat_config['template_id'] self.access_token = '' # 獲取access_token def get_access_token(self, appid, appsecret): url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (appid, appsecret) request = urllib2.Request(url) response = urllib2.urlopen(request) json_data = response.read() data = json.loads(json_data) access_token = data['access_token'] self.access_token = access_token return self.access_token # 獲取用戶(hù)列表 def get_user_list(self): if self.access_token == '': self.get_access_token(self.appid, self.appsecret) access_token = self.access_token url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=' % str(access_token) request = urllib2.Request(url) response = urllib2.urlopen(request) result = response.read() return json.loads(result) # 發(fā)送消息 def send_msg(self, openid, template_id, iciba_everyday): msg = { 'touser': openid, 'template_id': template_id, 'url': iciba_everyday['fenxiang_img'], 'data': { 'content': { 'value': iciba_everyday['content'], 'color': '#0000CD' }, 'note': { 'value': iciba_everyday['note'], }, 'translation': { 'value': iciba_everyday['translation'], } } } json_data = json.dumps(msg) if self.access_token == '': self.get_access_token(self.appid, self.appsecret) access_token = self.access_token url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s' % str(access_token) request = urllib2.Request(url, data=json_data) response = urllib2.urlopen(request) result = response.read() return json.loads(result) # 獲取愛(ài)詞霸每日一句 def get_iciba_everyday(self): url = 'http://open.iciba.com/dsapi/' request = urllib2.Request(url) response = urllib2.urlopen(request) json_data = response.read() data = json.loads(json_data) return data # 為設(shè)置的用戶(hù)列表發(fā)送消息 def send_everyday_words(self, openids): everyday_words = self.get_iciba_everyday() for openid in openids: result = self.send_msg(openid, self.template_id, everyday_words) if result['errcode'] == 0: print ' [INFO] send to %s is success' % openid else: print ' [ERROR] send to %s is error' % openid # 執(zhí)行 def run(self, openids=[]): if openids == []: # 如果openids為空,則遍歷用戶(hù)列表 result = self.get_user_list() openids = result['data']['openid'] # 根據(jù)openids對(duì)用戶(hù)進(jìn)行群發(fā) self.send_everyday_words(openids) if __name__ == '__main__': # 微信配置 wechat_config = { 'appid': 'xxxxx', #(No.1)此處填寫(xiě)你的appid 'appsecret': 'xxxxx', #(No.2)此處填寫(xiě)你的appsecret 'template_id': 'xxxxx' #(No.3)此處填寫(xiě)你的模板消息ID } # 用戶(hù)列表 openids = [ 'xxxxx', #(No.4)此處填寫(xiě)你的微信號(hào)(微信公眾平臺(tái)上你的微信號(hào)) #'xxxxx', #如果有多個(gè)用戶(hù)也可以 #'xxxxx', ] # 執(zhí)行 icb = iciba(wechat_config) # run()方法可以傳入openids列表,也可不傳參數(shù) # 不傳參數(shù)則對(duì)微信公眾號(hào)的所有用戶(hù)進(jìn)行群發(fā) icb.run()
Python3實(shí)現(xiàn)
#!/usr/bin/python3 #coding=utf-8 import json import requests class iciba: # 初始化 def __init__(self, wechat_config): self.appid = wechat_config['appid'] self.appsecret = wechat_config['appsecret'] self.template_id = wechat_config['template_id'] self.access_token = '' # 獲取access_token def get_access_token(self, appid, appsecret): url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=%s&secret=%s' % (str(appid), str(appsecret)) r = requests.get(url) data = json.loads(r.text) access_token = data['access_token'] self.access_token = access_token return self.access_token # 獲取用戶(hù)列表 def get_user_list(self): if self.access_token == '': self.get_access_token(self.appid, self.appsecret) access_token = self.access_token url = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=' % str(access_token) r = requests.get(url) return json.loads(r.text) # 發(fā)送消息 def send_msg(self, openid, template_id, iciba_everyday): msg = { 'touser': openid, 'template_id': template_id, 'url': iciba_everyday['fenxiang_img'], 'data': { 'content': { 'value': iciba_everyday['content'], 'color': '#0000CD' }, 'note': { 'value': iciba_everyday['note'], }, 'translation': { 'value': iciba_everyday['translation'], } } } json_data = json.dumps(msg) if self.access_token == '': self.get_access_token(self.appid, self.appsecret) access_token = self.access_token url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=%s' % str(access_token) r = requests.post(url, json_data) return json.loads(r.text) # 獲取愛(ài)詞霸每日一句 def get_iciba_everyday(self): url = 'http://open.iciba.com/dsapi/' r = requests.get(url) return json.loads(r.text) # 為設(shè)置的用戶(hù)列表發(fā)送消息 def send_everyday_words(self, openids): everyday_words = self.get_iciba_everyday() for openid in openids: result = self.send_msg(openid, self.template_id, everyday_words) if result['errcode'] == 0: print (' [INFO] send to %s is success' % openid) else: print (' [ERROR] send to %s is error' % openid) # 執(zhí)行 def run(self, openids=[]): if openids == []: # 如果openids為空,則遍歷用戶(hù)列表 result = self.get_user_list() openids = result['data']['openid'] # 根據(jù)openids對(duì)用戶(hù)進(jìn)行群發(fā) self.send_everyday_words(openids) if __name__ == '__main__': # 微信配置 wechat_config = { 'appid': 'xxxxx', #(No.1)此處填寫(xiě)你的appid 'appsecret': 'xxxxx', #(No.2)此處填寫(xiě)你的appsecret 'template_id': 'xxxxx' #(No.3)此處填寫(xiě)你的模板消息ID } # 用戶(hù)列表 openids = [ 'xxxxx', #(No.4)此處填寫(xiě)你的微信號(hào)(微信公眾平臺(tái)上你的微信號(hào)) #'xxxxx', #如果有多個(gè)用戶(hù)也可以 #'xxxxx', ] # 執(zhí)行 icb = iciba(wechat_config) # run()方法可以傳入openids列表,也可不傳參數(shù) # 不傳參數(shù)則對(duì)微信公眾號(hào)的所有用戶(hù)進(jìn)行群發(fā) icb.run()
本程序的GitHub地址:https://github.com/varlemon/wechat-iciba-everyday
測(cè)試程序
在Linux上執(zhí)行程序
在手機(jī)上查看,已經(jīng)收到了每日一句的消息
部署程序
在Linux上設(shè)置定時(shí)任務(wù)
crontab -e
添加如下內(nèi)容
0 6 * * * python /root/python/iciba/main-v1.0.py
注:以上內(nèi)容的含義是,在每天6:00
的時(shí)候,執(zhí)行這個(gè)Python程序
查看定時(shí)任務(wù)是否設(shè)置成功
crontab -l
至此,程序部署完成,請(qǐng)您明天6:00
查收吧!
效果圖如下
以上是“怎么利用Python將每日一句定時(shí)推送至微信”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。