微信公众号达到500粉丝,就可以申请成为流量主。在发布文章时平台会自动插入广告,有浏览和点击就会产生收益。
一、如何开通流量主
1、开通流量主
进入广告与服务=》流量主模块
2、启用开发者密钥
3、保存密钥
密钥一定要自己保存下来,平台不会保存,忘记重置之前的密钥就会失效!
4、配置白名单
- 查看自己的IP:www.ipshudi.com
- 点击IP白名单=》查看=》写入自己的IP(建议配置常用网络,可能需要几分钟才生效)
二、如何获取收益
公众号的收益在微信公众平台查看,如果有多个微信公众号,需要频繁扫码登录。公众平台提供了丰富的API,作为一名程序员可以直接通过代码获取具体收益。
1、配置公众号信息
将公众号名称、ID、密钥保存到一个csv文件,格式如下:
# 读取公众号配置文件
def get_accounts(filename):
apps = []
with open(filename, mode='r', encoding='utf-8') as file:
reader = csv.DictReader(file)
for row in reader:
apps.append({
'app_name': row['app_name'],
'app_id': row['app_id'],
'app_secret': row['app_secret']
})
return apps
2、获取token
公众平台所有API请求都需要使用token,有效期2个小时,失效之前每次请求返回的token相同
# 获取token
def get_access_token(app_id, app_secret):
url = f"https://api.weixin.qq.com/cgi-bin/stable_token"
payload = {
"grant_type": "client_credential",
"appid": app_id,
"secret": app_secret,
"force_refresh": False
}
headers = {'Content-Type': 'application/json'}
response = requests.post(url, data=json.dumps(payload), headers=headers)
data = response.json()
access_token = data.get("access_token", "")
return access_token
3、获取收入明细
每次默认返回10条数据,需要分页请求
# 获取广告收入
def get_ad_statistics(account_name, access_token, start_date, end_date):
url = "https://api.weixin.qq.com/publisher/stat"
page = 1
page_size = 10
file_path = "流量主账号财务统计.csv"
while True:
params = {
"action": "publisher_adpos_general",
"access_token": access_token,
"page": page,
"page_size": page_size,
"start_date": start_date,
"end_date": end_date
}
try:
response = requests.get(url, params=params)
response.raise_for_status() # 检查请求是否成功
data = response.json()
list = data.get("list",[])
# 没有任何数据
if not list:
break
append_data_to_csv(account_name, file_path, page, list)
total_num = data.get("total_num", 0)
total_pages = math.ceil(total_num / page_size)
if page >= total_pages:
break
page += 1
except Exception as e:
print(f"Request error: {e}")
break
4、记录文件
将广告位置、日期、收入、eCMP、点击率、点击量、曝光量记录到文件
广告位:
SLOT_ID_BIZ_BOTTOM | 公众号底部广告 |
SLOT_ID_BIZ_MID_CONTEXT | 公众号文中广告 |
SLOT_ID_BIZ_VIDEO_END | 公众号视频后贴 |
SLOT_ID_BIZ_SPONSOR | 公众号互选广告 |
SLOT_ID_BIZ_CPS | 公众号返佣商品 |
# 写入文件
def append_data_to_csv(account_name, file_path, page, list):
# 检查文件是否存在以决定是否写入表头
if not os.path.exists(file_path):
with open(file_path, mode='w', newline='', encoding='utf-8') as file:
writer = csv.writer(file)
writer.writerow(['账号名称', '广告位置', '日期', '总收入(分)', 'eCPM(分)', '点击率', '点击量', '曝光量'])
index = 0
# 使用追加模式写入文件
with open(file_path, mode='a', newline='', encoding='utf-8') as file:--
writer = csv.writer(file)
for item in list:
index += 1
account_name = account_name if (page == 1 and index == 1) else ""
ad_slot = item.get('ad_slot', '')
ad_slot_name = ad_slot_config.get(ad_slot, '官方未提供')
writer.writerow([
account_name, # 账号名称
ad_slot_name, # 广告位置
item.get('date', ''), # 日期
item.get('income', ''), # 总收入
item.get('ecpm', ''), # eCPM
item.get('click_rate', ''), # 点击率
item.get('click_count', ''), # 点击量
item.get('exposure_count', '') # 曝光量
三、API文档
AI总结鸭支持微信群消息、公众号文章、网页、PDF、图片一键总结,关注公众号立即领取!