encoding=utf-8
import requests
import json
import urllib.request
import datetime
import time
定义时间参数
time_now = datetime.datetime.now()
now_d = time.strftime("%Y-%m-%d")
ytd_d = (time_now + datetime.timedelta(days=-1)).strftime("%Y-%m-%d")
print(ytd_d)
def get_content(url):
‘获取网页内容’
html = urllib.request.urlopen(url)
content = html.read()
html.close()
return content
def getAccess_token(AppID,AppSecret):
‘获取微信唯一标识ACCESS_TOKEN,access_token是公众号的全局唯一票据’
url=“https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=”+AppID+"&secret="+AppSecret
info=get_content(url)
access_token=json.loads(info)[“access_token”]
# print(access_token)
return access_token
def getUser(access_token,ytd_d):
“用户分析数据接口”
r_date = {‘begin_date’:ytd_d, ‘end_date’:ytd_d}
# 获取累计用户数据
cumulate = requests.post("https://api.weixin.qq.com/datacube/getusercumulate?access_token=" + access_token,
json=r_date)
getusercumulate = cumulate.json()
for _ in getusercumulate['list']:
ref_date = (_['ref_date'])
cumulate_user = (_['cumulate_user'])
print(ref_date,cumulate_user)
if name == “main”:
#公众号信息
AppID="你的公众号" ##填写你的公众号信息
AppSecret="你的公众号" #填写你的公众号信息
access_token=getAccess_token(AppID,AppSecret)
getUser(access_token,ytd_d)