使用 Python 實(shí)現(xiàn)微信公眾號(hào)粉絲遷移流程
近日,因公司業(yè)務(wù)需要,需將原兩個(gè)公眾號(hào)合并為一個(gè),即要將其中一個(gè)公眾號(hào)(主要是粉絲)遷移到另一個(gè)公眾號(hào)。按微信規(guī)范,同一用戶在不同公眾號(hào)內(nèi)的 openid 是不同的,我們的業(yè)務(wù)系統(tǒng)不例外地記錄了用戶的 openid,因此,涉及到兩個(gè)公眾號(hào)的 openid 的轉(zhuǎn)換。幸好,微信公眾號(hào)平臺(tái)在賬號(hào)遷移描述提供了方法和API供調(diào)用,詳見(jiàn):
http://kf.qq.com/faq/170221aUnmmU170221eUZJNf.html
這里使用 Python 寫個(gè)程序來(lái)完成,簡(jiǎn)單快捷,主要知識(shí)點(diǎn)有:
- MySQL connector 使用,也就是 Python DB API 規(guī)范
- HTTP客戶端庫(kù) requests 使用
- 微信公眾號(hào)平臺(tái) API 使用
首先,建立新舊 openid 對(duì)照表。
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 ;
如果沒(méi)有安裝,則需先安裝以下類庫(kù)。
pip install mysql-connector-python pip install requests
接著,運(yùn)行下面 python 程序,即可將新舊 openid 對(duì)照數(shù)據(jù)寫到 change_openidlist,然后就可以根據(jù)這個(gè)表的數(shù)據(jù)去更新其它數(shù)據(jù)表了。
其它可見(jiàn)注釋,不詳述,當(dāng)然不要忘了將 appid 和 secret 替換為自己公眾號(hào)。
# -*- coding: utf-8 -*- import requests import mysql.connector def handle_data(): try: token = get_access_token() #自動(dòng)提交方式 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() #舊公眾號(hào) 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()
總結(jié)
以上所述是小編給大家介紹的使用 Python 實(shí)現(xiàn)微信公眾號(hào)粉絲遷移流程,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
Python函數(shù)元數(shù)據(jù)實(shí)現(xiàn)為一個(gè)參數(shù)指定多個(gè)類型
這篇文章主要介紹了Python函數(shù)元數(shù)據(jù)實(shí)現(xiàn)為一個(gè)參數(shù)指定多個(gè)類型方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2024-02-02python爬蟲開發(fā)之PyQuery模塊詳細(xì)使用方法與實(shí)例全解
這篇文章主要介紹了python爬蟲開發(fā)之PyQuery模塊詳細(xì)使用方法與實(shí)例全解,需要的朋友可以參考下2020-03-03django 連接數(shù)據(jù)庫(kù) sqlite的例子
今天小編就為大家分享一篇django 連接數(shù)據(jù)庫(kù) sqlite的例子,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-08-08pandas中DataFrame的merge操作的實(shí)現(xiàn)
本文主要介紹了pandas中DataFrame的merge操作的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2024-07-07keras模型可視化,層可視化及kernel可視化實(shí)例
今天小編就為大家分享一篇keras模型可視化,層可視化及kernel可視化實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2020-01-01Python標(biāo)準(zhǔn)模塊--ContextManager上下文管理器的具體用法
本篇文章主要介紹了Python標(biāo)準(zhǔn)模塊--ContextManager的具體用法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-11-11