溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊(cè)×
其他方式登錄
點(diǎn)擊 登錄注冊(cè) 即表示同意《億速云用戶(hù)服務(wù)條款》

怎么利用Python將每日一句定時(shí)推送至微信

發(fā)布時(shí)間:2021-04-25 10:32:24 來(lái)源:億速云 閱讀:481 作者:小新 欄目:開(kāi)發(fā)技術(shù)

這篇文章主要介紹怎么利用Python將每日一句定時(shí)推送至微信,文中介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們一定要看完!

Python的優(yōu)點(diǎn)有哪些

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)境

調(diào)用地址:http://open.iciba.com/dsapi/

請(qǐng)求方式:GET

請(qǐng)求參數(shù):

參數(shù)必選類(lèi)型說(shuō)明
datestring格式為:2013-05-06;如果date為,則默認(rèn)取當(dāng)天
typestring可選值為last和next;以date日期為準(zhǔn)的,last返回前一天的,next返回后一天的

返回類(lèi)型:JSON

JSON字段解釋?zhuān)?/p>

屬性名屬性值類(lèi)型說(shuō)明
sidstring每日一句ID
ttsstring音頻地址
contentstring英文內(nèi)容
notestring中文內(nèi)容
lovestring每日一句喜歡個(gè)數(shù)
translationstring詞霸小編
picturestring圖片地址
picture2string大圖片地址
captionstring標(biāo)題
datelinestring時(shí)間
s_pvstring瀏覽數(shù)
sp_pvstring語(yǔ)音評(píng)測(cè)瀏覽數(shù)
tagsarray相關(guān)標(biāo)簽
fenxiang_imgstring合成圖片,建議分享微博用的

正常返回示例:

{
 "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
怎么利用Python將每日一句定時(shí)推送至微信

手機(jī)上確認(rèn)登錄

怎么利用Python將每日一句定時(shí)推送至微信

找到新增測(cè)試模板,添加模板消息

怎么利用Python將每日一句定時(shí)推送至微信

填寫(xiě)模板標(biāo)題每日一句,填寫(xiě)如下模板內(nèi)容

{{content.DATA}}

{{note.DATA}}

{{translation.DATA}}

怎么利用Python將每日一句定時(shí)推送至微信

提交保存之后,記住該模板ID,一會(huì)兒會(huì)用到

怎么利用Python將每日一句定時(shí)推送至微信

找到測(cè)試號(hào)信息,記住appid和appsecret,一會(huì)兒會(huì)用到

怎么利用Python將每日一句定時(shí)推送至微信

找到測(cè)試號(hào)二維碼。手機(jī)掃描此二維碼,關(guān)注之后,你的昵稱(chēng)會(huì)出現(xiàn)在右側(cè)列表里,記住該微信號(hào),一會(huì)兒會(huì)用到(注:此微信號(hào)非你真實(shí)的微信號(hào))

怎么利用Python將每日一句定時(shí)推送至微信

發(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í)行程序

怎么利用Python將每日一句定時(shí)推送至微信

在手機(jī)上查看,已經(jīng)收到了每日一句的消息

怎么利用Python將每日一句定時(shí)推送至微信

部署程序

在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í)推送至微信

以上是“怎么利用Python將每日一句定時(shí)推送至微信”這篇文章的所有內(nèi)容,感謝各位的閱讀!希望分享的內(nèi)容對(duì)大家有幫助,更多相關(guān)知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!

向AI問(wèn)一下細(xì)節(jié)

免責(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)容。

AI