资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

使用Python怎么迁移微信公众号粉丝-创新互联

使用 Python 怎么迁移微信公众号粉丝?针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

创新互联公司是一家专业提供东昌企业网站建设,专注与成都网站建设、网站制作、成都h5网站建设、小程序制作等业务。10年已为东昌众多企业、政府机构等服务。创新互联专业的建站公司优惠进行中。

首先,建立新旧 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 怎么迁移微信公众号粉丝问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注创新互联成都网站设计公司行业资讯频道了解更多相关知识。

另外有需要云服务器可以了解下创新互联scvps.cn,海内外云服务器15元起步,三天无理由+7*72小时售后在线,公司持有idc许可证,提供“云服务器、裸金属服务器、高防服务器、香港服务器、美国服务器、虚拟主机、免备案服务器”等云主机租用服务以及企业上云的综合解决方案,具有“安全稳定、简单易用、服务可用性高、性价比高”等特点与优势,专为企业上云打造定制,能够满足用户丰富、多元化的应用场景需求。


本文名称:使用Python怎么迁移微信公众号粉丝-创新互联
标题URL:http://cdkjz.cn/article/ccgsih.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220