赞
踩
目录
微信公众号平台:微信公众平台 (qq.com)
微信公众号开发文档:https://developers.weixin.qq.com/doc/offiaccount/Getting_Started/Overview.html
1、微信公众号注册:https://mp.weixin.qq.com/debug/cgi-bin/sandbox?t=sandbox/login
2、注册成功后可生成属于自己的appID和appsecret
3、想要查看推送的效果,需先关注当前的测试账号,关注成功后,可在列表查看当前的粉丝数和具体的open_id
1、获取微信公众号的授权token:https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={appid}&secret={appsecret}
2、获取当前公众号的粉丝的open_id:https://api.weixin.qq.com/cgi-bin/user/get?access_token={self.token}&next_openid={next_openid}
3、 发送模板消息的接口:https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={self.token}
4、发送普通消息的接口:https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=xxx
5、其他构造数据的接口推荐
天气网 (weather.com.cn) 使用爬虫进行获取
(10条消息) 中国天气城市代码编号_101010100_it_zujun的博客-CSDN博客
‘’使用方法:f'http://www.weather.com.cn/weather{城市编号‘’使用方法:'http://www.weather.com.cn/weather{}/.shtml'
https://api.lovelive.tools/api/SweetNothings/Serialization/Json 每日情话
- import json, random, requests
-
-
- class WechatSendmes():
-
- def __init__(self,appid,secret,template_id):
- self.appid = appid
- self.secret = secret
- self.template_id = template_id
-
- def get_token(self):
- '''
- # 获取access_token
- :return:
- '''
- url = 'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(
- self.appid, self.secret)
- headers = {
- 'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_6) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.67 Safari/537.36'
- }
- res = requests.get(url, headers=headers).json()
- access_token = res['access_token']
- return access_token
-
- def get_openid(self):
- '''
- # 获取要推送用户的openid
- :return:
- '''
- next_openid = '' # 第一个拉取的OPENID,不填默认从头开始拉取 ,一次只能获取10000条
- url_openid = 'https://api.weixin.qq.com/cgi-bin/user/get?access_token=%s&next_openid=%s' % (
- self.get_token(), next_openid)
- ans = requests.get(url_openid)
- openid = json.loads(ans.content)['data']['openid']
- return openid
-
- def get_shici(self):
- '''
- # 随机诗词句获取
- :return:
- '''
- url = 'https://v1.jinrishici.com/'
- headers = {"Content-Type": "application/json; charset=utf-8"}
- res = requests.get(url, headers=headers)
- api_lists = res.json()["list"]
- url_apis = api_lists[random.randint(0, len(api_lists))]
- url_api = list(url_apis.values())[0]
- res_shici = requests.get(url=url_api, headers=headers)
- shici_data = res_shici.json()
- zuozhe = shici_data["author"]
- shi_name = shici_data["origin"]
- nr = shici_data["content"]
- return zuozhe, shi_name, nr
-
- def sendmes(self):
- '''
- 推送到微信公众号
- :return:
- '''
- author, origin, content = self.get_shici()
- url = 'https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={}'.format(self.get_token())
- headers = {"Content-type": "application/json"}
- for index, template_id in enumerate(self.get_openid()):
- body = {
- "touser": template_id, # 接收内容的用户openid
- "template_id": self.template_id, # 模板ID
- "url": f"https://baike.baidu.com/item/{origin}", # 可跳转的链接
- # "topcolor": "#FF0000",
- "data": {
- "city": {"value": '中国-西安', "color": '#00EC00'}, # 颜色功能已失效,2023年5月4日已改版,不支持颜色
- 'author': {'value': author},
- 'origin': {'value': origin},
- 'content': {'value': content}
- }
- }
- res = requests.post(url, json=body, headers=headers)
- print('推送成功') if res.json()['errmsg'] == 'ok' else print(f'用户{template_id}推送失败')
-
-
- if __name__ == '__main__':
- push = WechatSendmes(appid_data,secret_data,template_id_data)
- push.sendmes()
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。