您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

Python实现将爱词霸每日一句定时推送至微信

5b51 2022/1/14 8:24:22 python 字数 120482 阅读 671 来源 www.jb51.cc/python

基于微信公众平台测试号开发一个定时给用户发送“每日一句”功能的程序。每日一句的数据源调用“爱词霸每日一句”的API,内容包含英文及释义还有小编的解读。发送的微信消息使用微信公众号的模板消息。定时功能是

概述

<h2 id="前言">前言

前几天在网上看到一篇文章《教你用微信每天给女票说晚安》,感觉很神奇的样子,随后研究了一下,构思的确是巧妙。好,那就开始动工吧!服务器有了,Python环境有了,IDE打开了...然而...然而...我意识到了一个非常严重的问题...没有女朋友 (T_T)...

微信开发已经活跃了很长时间了,在微信开发中有一个神奇的接口它叫模板消息接口,它可以根据用户的openid从服务端给用户推送自定义的模板消息,正因如此,我们可以利用这个特征在服务器端随时向用户推送消息(前提是该用户关注了该公众号)。

总结出3点,1.模板消息的格式可以自定义,2.模板消息的内容可以自定义,3.模板消息发送的时间可以自定义。那么我们可以利用这些性质为自己做一款说早安的程序啦!

  1. 阿里云Linux服务器
  2. Python环境

调用地址:http://open.iciba.com/dsapi/ 请求方式:GET 请求参数:

参数 必选 类型 说明 2013-05-06;如果 date为空,则 认取当天last和 next;以 date日期为准的, last返回前一天的, next返回后一天的 返回类型:JSON JSON字段解释:

属性属性值类型 说明 内容中文 内容图片地址图片地址标题标签图片,建议 分享微博用的 正常返回示例:

{
  "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": "不管多努力蜕变成熟,一旦受伤哭泣时,我们还是像个孩子。","love": "1966","translation": "小编的话:这句话出自小说《彼得·潘》。岁月永远年轻,我们慢慢老去。不管你如何蜕变,最后你会发现:童心未泯,是一件值得骄傲的事情。长大有时很简单,但凡事都能抱着一颗童心去快乐享受却未必容易。","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"
}

请求示例:

Python2请求示例

#!/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请求示例

#!/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请求示例

PHP">
   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,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();

本接口(每日一句)官方文档: 参考资料:

登录微信公众平台接口测试账号">登录微信公众平台接口测试账号

扫描登录公众平台测试号

手机上确认登录

找到新增测试模板,添加模板消息

填写模板标题每日一句,填写如下模板内容

{{content.DATA}}

{{note.DATA}}

{{translation.DATA}}

提交保存之后,记住该模板ID,一会儿会用到

找到测试号信息,记住appidappsecret,一会儿会用到

找到测试号二维码。手机扫描此二维码,关注之后,你的昵称会出现在右侧列表里,记住该微信号,一会儿会用到(注:此微信号非你真实的微信号)

本程序的GitHub地址:

本程序您只需要修改4个地方即可,请看注释

Python2实现

#!/usr/bin/python2
#coding=utf-8
import json
import urllib2

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)

if name == 'main':

微信配置

wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

python3实现

#!/usr/bin/python3
#coding=utf-8
import json
import requests

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()


<h2 id="测试程序">测试程序

在Linux上执行程序

在手机上查看,已经收到了每日一句的消息

登录微信公众平台接口测试账号">登录微信公众平台接口测试账号

本程序的GitHub地址:

本程序您只需要修改4个地方即可,请看注释

Python2实现

#!/usr/bin/python2
#coding=utf-8
import json
import urllib2

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)

if name == 'main':

微信配置

wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

python3实现

#!/usr/bin/python3
#coding=utf-8
import json
import requests

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()


<h2 id="测试程序">测试程序

{
  "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": "不管多努力蜕变成熟,一旦受伤哭泣时,我们还是像个孩子。","love": "1966","translation": "小编的话:这句话出自小说《彼得·潘》。岁月永远年轻,我们慢慢老去。不管你如何蜕变,最后你会发现:童心未泯,是一件值得骄傲的事情。长大有时很简单,但凡事都能抱着一颗童心去快乐享受却未必容易。","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"
}
#!/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()
#!/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">
  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,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();
{{content.DATA}}

{{note.DATA}}

{{translation.DATA}}

#!/usr/bin/python2
#coding=utf-8
import json
import urllib2

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)

if name == 'main':

微信配置

wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)
wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()
#!/usr/bin/python3
#coding=utf-8
import json
import requests

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

调用地址:http://open.iciba.com/dsapi/ 请求方式:GET 请求参数:

请求示例:

Python2请求示例

python3请求示例

PHP请求示例

本接口(每日一句)官方文档: 参考资料:

扫描登录公众平台测试号

手机上确认登录

找到新增测试模板,添加模板消息

填写模板标题每日一句,填写如下模板内容

{{note.DATA}}

{{translation.DATA}}

提交保存之后,记住该模板ID,一会儿会用到

找到测试号信息,记住appidappsecret,一会儿会用到

找到测试号二维码。手机扫描此二维码,关注之后,你的昵称会出现在右侧列表里,记住该微信号,一会儿会用到(注:此微信号非你真实的微信号)

本程序的GitHub地址:

本程序您只需要修改4个地方即可,请看注释

Python2实现

class iciba:

if name == 'main':

python3实现

class iciba:

在Linux上执行程序

在手机上查看,已经收到了每日一句的消息

调用地址:http://open.iciba.com/dsapi/ 请求方式:GET 请求参数:

{
  "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": "不管多努力蜕变成熟,一旦受伤哭泣时,我们还是像个孩子。","love": "1966","translation": "小编的话:这句话出自小说《彼得·潘》。岁月永远年轻,我们慢慢老去。不管你如何蜕变,最后你会发现:童心未泯,是一件值得骄傲的事情。长大有时很简单,但凡事都能抱着一颗童心去快乐享受却未必容易。","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"
}

请求示例:

Python2请求示例

#!/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请求示例

#!/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请求示例

PHP">
  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,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();

本接口(每日一句)官方文档: 参考资料:

登录微信公众平台接口测试账号">登录微信公众平台接口测试账号

扫描登录公众平台测试号

手机上确认登录

找到新增测试模板,添加模板消息

填写模板标题每日一句,填写如下模板内容

{{content.DATA}}

{{note.DATA}}

{{translation.DATA}}

{{note.DATA}}

{{translation.DATA}}

{{note.DATA}}

{{translation.DATA}}

提交保存之后,记住该模板ID,一会儿会用到

找到测试号信息,记住appidappsecret,一会儿会用到

找到测试号二维码。手机扫描此二维码,关注之后,你的昵称会出现在右侧列表里,记住该微信号,一会儿会用到(注:此微信号非你真实的微信号)

本程序的GitHub地址:

本程序您只需要修改4个地方即可,请看注释

Python2实现

#!/usr/bin/python2
#coding=utf-8
import json
import urllib2

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)

if name == 'main':

微信配置

wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

python3实现

#!/usr/bin/python3
#coding=utf-8
import json
import requests

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()


<h2 id="测试程序">测试程序

#!/usr/bin/python2
#coding=utf-8
import json
import urllib2

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)

if name == 'main':

微信配置

wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)
wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()
#!/usr/bin/python3
#coding=utf-8
import json
import requests

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

本程序的GitHub地址:

本程序您只需要修改4个地方即可,请看注释

Python2实现

class iciba:

if name == 'main':

python3实现

class iciba:

本程序的GitHub地址:

本程序您只需要修改4个地方即可,请看注释

Python2实现

#!/usr/bin/python2
#coding=utf-8
import json
import urllib2

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)

if name == 'main':

微信配置

wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)
wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

class iciba:

if name == 'main':

class iciba:

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>):
    self.appid = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appid']
    self.appsecret = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['appsecret']
    self.template_id = wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>['template_id']
    self.access_token = ''

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>access_token
def get_access_token(self,appid,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    request = urllib2.Request(url)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# 发送消息
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    request = urllib2.Request(url,data=json_data)
    response = urllib2.urlopen(request)
    result = response.read()
    return json.loads(result)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
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

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
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 <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid
        else:
            print ' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid

# 执行
def run(self,openids=[]):
    if openids == []:
        # 如果openids为空,则遍历<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
        result = self.get_user_list()
        openids = result['data']['openid']
    # 根据openids对<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
    self.send_everyday_words(openids)

if name == 'main':

wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a> = {
    'appid': 'xxxxx',#(No.1)此处填写你的appid
    'appsecret': 'xxxxx',#(No.2)此处填写你的appsecret
    'template_id': 'xxxxx' #(No.3)此处填写你的模板消息ID
}

# <a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
openids = [
    'xxxxx',#(No.4)此处填写你的微信号(微信公众平台上你的微信号)
    #'xxxxx',#如果有多个<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>也可以
    #'xxxxx',]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

python3实现

#!/usr/bin/python3
#coding=utf-8
import json
import requests

class iciba:

初始化

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

class iciba:

class iciba:

def <a href="https://www.jb51.cc/tag/init/" target="_blank" class="keywords">__init__</a>(self,appsecret):
    url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&amp;appid=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;secret=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % (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

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a><a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表
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=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>&amp;next_openid=' % str(access_token)
    r = requests.get(url)
    return json.loads(r.text)

# 发送消息
def send_msg(self,self.appsecret)
    access_token = self.access_token
    url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=<a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a>' % str(access_token)
    r = requests.post(url,json_data)
    return json.loads(r.text)

# <a href="https://www.jb51.cc/tag/huoqu/" target="_blank" class="keywords">获取</a>爱词霸每日一句
def get_iciba_everyday(self):
    url = 'http://open.iciba.com/dsapi/'
    r = requests.get(url)
    return json.loads(r.text)

# 为设置的<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>列表发送消息
def send_everyday_words(self,everyday_words)
        if result['errcode'] == 0:
            print (' [INFO] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is success' % openid)
        else:
            print (' [ERROR] send to <a href="https://www.jb51.cc/tag/s/" target="_blank" class="keywords">%s</a> is error' % openid)

# 执行
def run(self,]


# 执行
icb = iciba(wechat_con<a href="https://www.jb51.cc/tag/fig/" target="_blank" class="keywords">fig</a>)

# run()<a href="https://www.jb51.cc/tag/fangfa/" target="_blank" class="keywords">方法</a>可以传入openids列表,也可不传参数
# 不传参数则对微信公众号的所有<a href="https://www.jb51.cc/tag/yonghu/" target="_blank" class="keywords">用户</a>进行群发
icb.run()

在Linux上执行程序

在手机上查看,已经收到了每日一句的消息

在Linux上设置定时任务

crontab -e

添加如下内容

0 6 * * *    python /root/python/iciba/main-v1.0.py

注:以上内容的含义是,在每天6:00的时候,执行这个Python程序 查看定时任务是否设置成功

crontab -l

至此,程序部署完成,请您明天6:00查收吧! 效果图如下

本文链接

crontab -e
0 6 * * *    python /root/python/iciba/main-v1.0.py
crontab -l

在Linux上设置定时任务

添加如下内容

注:以上内容的含义是,在每天6:00的时候,执行这个Python程序 查看定时任务是否设置成功

至此,程序部署完成,请您明天6:00查收吧! 效果图如下

本文链接

在Linux上设置定时任务

crontab -e

添加如下内容

0 6 * * *    python /root/python/iciba/main-v1.0.py

注:以上内容的含义是,在每天6:00的时候,执行这个Python程序 查看定时任务是否设置成功

crontab -l

至此,程序部署完成,请您明天6:00查收吧! 效果图如下

本文链接

总结

以上是编程之家为你收集整理的Python实现将爱词霸每日一句定时推送至微信全部内容,希望文章能够帮你解决Python实现将爱词霸每日一句定时推送至微信所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶