如何获取公众号下面粉丝的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))