使用 Python 实现微信公众号粉丝迁移流程

yipeiwu_com5年前Python基础

近日,因公司业务需要,需将原两个公众号合并为一个,即要将其中一个公众号(主要是粉丝)迁移到另一个公众号。按微信规范,同一用户在不同公众号内的 openid 是不同的,我们的业务系统不例外地记录了用户的 openid,因此,涉及到两个公众号的 openid 的转换。幸好,微信公众号平台在账号迁移描述提供了方法和API供调用,详见:

http://kf.qq.com/faq/170221aUnmmU170221eUZJNf.html

这里使用 Python 写个程序来完成,简单快捷,主要知识点有:

  • MySQL connector 使用,也就是 Python DB API 规范
  • HTTP客户端库 requests 使用
  • 微信公众号平台 API 使用

首先,建立新旧 openid 对照表。

CREATE TABLE change_openidlist(
  id BIGINT NOT NULL AUTO_INCREMENT,
  ori_openid varchar(100) NOT NULL,
  new_openid varchar(100) NOT NULL,
  CONSTRAINT crm_change_openidlist_pk PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci ;

如果没有安装,则需先安装以下类库。

pip install mysql-connector-python
pip install requests

接着,运行下面 python 程序,即可将新旧 openid 对照数据写到 change_openidlist,然后就可以根据这个表的数据去更新其它数据表了。

其它可见注释,不详述,当然不要忘了将 appid 和 secret 替换为自己公众号。

# -*- coding: utf-8 -*-
import requests
import mysql.connector
def handle_data():
  try:
    token = get_access_token()
    #自动提交方式 autocommit=True
    conn = mysql.connector.connect(host='127.0.0.1', port='3306', user='user', password='password', database='wx', use_unicode=True,autocommit=True);
    qcursor = conn.cursor(buffered=True)
    wcursor = conn.cursor()
    #旧公众号 openid
    qcursor.execute('select openid from wxmembers')
    size = 100
    while True:
      list = qcursor.fetchmany(size)
      if not list:
        break
      changeopenid_list = get_changeopenid_list(list,token)
      wcursor.executemany('insert into change_openidlist (ori_openid,new_openid) values (%s, %s)',changeopenid_list)
  except mysql.connector.Error as e:
    print ('Error : {}'.format(e))
  finally:
    qcursor.close
    wcursor.close()
    conn.close
    print 'openid handle finished!'
def get_access_token():
  new_appid = '00000'
  new_secret = '11111'
  url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential' # grant_type为固定值
  payload = {'appid': new_appid, 'secret': new_secret}
  r = requests.get(url,params = payload)
  response = r.json()
  return response['access_token']
def get_changeopenid_list(ori_openid_list,token):
  new_access_token = token
  ori_appid = '33333'
  url = 'http://api.weixin.qq.com/cgi-bin/changeopenid?access_token='+ new_access_token
  payload = {'to_appid': ori_appid, 'openid_list': ori_openid_list}
  r = requests.post(url,json = payload)
  response = r.json()
  result_list = response['result_list']
  openid_list = [[result['ori_openid'],result['new_openid']] for result in result_list if result['err_msg'] == 'ok']
  return openid_list
if __name__ == '__main__':
  handle_data()

总结

以上所述是小编给大家介绍的使用 Python 实现微信公众号粉丝迁移流程,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对【听图阁-专注于Python设计】网站的支持!

相关文章

如何利用pygame实现简单的五子棋游戏

如何利用pygame实现简单的五子棋游戏

前言 写程序已经丢掉很长一段时间了,最近觉得完全把技术丢掉可能是个死路,还是应该捡起来,所以打算借CSDN来记录学习过程, 由于以前没事的时候断断续续学习过python和用flask框...

tensorflow 只恢复部分模型参数的实例

我就废话不多说了,直接上代码吧! import tensorflow as tf def model_1(): with tf.variable_scope("var_a"):...

python3 requests库文件上传与下载实现详解

在接口测试学习过程中,遇到了利用requests库进行文件下载和上传的问题。同样,在真正的测试过程中,我们不可避免的会遇到上传和下载的测试。 文件上传: url = ztx.host...

Python+PIL实现支付宝AR红包

Python+PIL实现支付宝AR红包

本文实例为大家分享了Python+PIL处理支付宝AR红包的具体代码,供大家参考,具体内容如下 思路比较简单: 1、对图片进行锐化处理; 2、设(r_h, g_h, b_h)为支付宝遮...

python 参数列表中的self 显式不等于冗余

self在区分全局变量/函数和对象中的成员变量/函数十分有用。例如,它提供了一种作用域机制,我个人认为比Ruby的@和@@清晰多了,这可能是习惯使然吧,但它确实和C++、Java中的th...