1. 程式人生 > >python如何獲取公眾號下面粉絲的openid

python如何獲取公眾號下面粉絲的openid

ads cli col acc 操作 hose info 名單 req

如何獲取公眾號下面粉絲的openid呢,首先要獲取一個access_token,這個token可不是令牌(Token),如何獲取這個access_token呢?有兩種方法,方法如下:

# -*- coding: cp936 -*-
#python 27
#xiaodeng
#原文 https://www.cnblogs.com/dengyg200891/p/5094419.html

#獲取微信access_token
#辦法一:將該url直接填寫到瀏覽器地址中可以獲得access_token
url=https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=wxc0f4d0ed1cb2f4e1&secret=c4d72d33cacf8c94845ac906ad60eed6
#辦法二 import urllib url=https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential#grant_type為固定值 data={appid:wxc0f4d0ed1cb2f4e1, secret:c4d72d33cacf8c94845ac906ad60eed6} data=urllib.urlencode(data) html=urllib.urlopen(url,data)#<addinfourl at 34758208 whose fp = <socket._fileobject object at 0x0212F570>>
html= html.read() print html ‘‘‘ {"access_token":"Isd_tm3QE8Kateyxjz_WEEXuerBZ0gnO6XwyjirZXY1umVIDqebi6GK2Zv2fv1hI7sXQfHXeaOa2A4XrOITwS5LnczFRXf4BbSnMdSRLKiwBQYgADASHP", "expires_in":7200} #expires_in,定義access_token有效時間,單位為秒 ‘‘‘

註意:獲取access_token需要在微信公眾號管理匯總設置一下白名單,否則在本地無法獲取

接下來就可以進行獲取openid的操作了,下面代碼源自 https://blog.csdn.net/qq1752506968/article/details/81164947

import requests,json
access_token=相關公眾號的token‘ #未認證的訂閱號不能獲取,也就是沒有權限獲取粉絲openid
next_openid=‘‘
url=https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s%(access_token,next_openid)
ans=requests.get(url)
#print(ans.content)
a=json.loads(ans.content)[data][openid]
print (a,len(a))

python如何獲取公眾號下面粉絲的openid