个人微信公众号自动发布文章

  • A+
所属分类:编程语言

微信公众号发文有很多限制,特别是账号主体为个人的订阅号,没认证没法通过api接口直接编辑自动发送,那么如何才能实现个人微信公众号自动发布文章,群发给所有关注的粉丝呢?主要思路时先通过api接口发布文章到草稿,然后通过手动模拟群发,主要步骤如下:

一,微信公众号设置与开发的配置

浏览器登录微信公众号后在设置与开发菜单下的基本配置里面获取到开发者ID(AppID),开发者密码(AppSecret)以及设置IP白名单。虽然个人微信公众号不能通过api接口自动群发文章但是可以将文章自动发布为草稿后续再发送,(当然也可以直接发送但是不会被推荐通知粉丝,只是被调用查看,这不是想要的)。特别说明ip白名单一定要设置否则发送不被允许,ip白名单指的是公网ip地址。

二,编辑自动发布草稿模块

这里使用python主要实现了个人微信公众号的获取access_token 、发布图片、发布草稿、获取媒体等接口,编写好标题、内容、图片等模板,调用接口直接发送到草稿。至于说具体什么内容是否调用chatgpt来进行润色优化伪原创等等就不细说了,这里要做的是的如何实现自动化发布。

展开

  1. class?GZHApi(object):
  2. ????def?__init__(self,?AppID=None,AppSecret=None):
  3. ????????self.AppID?=?AppID?if?AppID?else?APPID
  4. ????????self.AppSecret?=??AppSecret?if?AppSecret?else?APPSECRET
  5. ????????self.access_token_path?=?os.path.join(os.path.dirname(__file__),f'{self.AppID}.token')
  6. ????????self.access_token?=?self.get_access_token()
  7. ????def?get_access_token(self):
  8. ????????if?os.path.isfile(self.access_token_path):
  9. ????????????with?open(self.access_token_path,'r')?as?f:
  10. ????????????????data?=?json.load(f)?or?dict()
  11. ????????????????expires_time?=?data.get('expires_time',0)
  12. ????????????????access_token?=?data.get('access_token','')
  13. ????????????if?(time.time()?-?expires_time?>=?7200)?or?not?access_token:
  14. ????????????????os.remove(self.access_token_path)
  15. ????????????????return?self.get_access_token()
  16. ????????????else:
  17. ????????????????return?access_token
  18. ????????access_token?=?''
  19. ????????try:
  20. ????????????url?=?'https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={}&secret={}'.format(
  21. ????????????????self.AppID,?self.AppSecret)
  22. ????????????response?=?requests.get(url)
  23. ????????????res_html?=?response.json()
  24. ????????????access_token?=?res_html['access_token']
  25. ????????????with?open(self.access_token_path,?'w')?as?f:
  26. ????????????????json.dump({'access_token':access_token,'expires_time':time.time()},f)
  27. ????????except?Exception?as?e:
  28. ????????????print(e)
  29. ????????return?access_token
  30. ????def?push_image(self,image_path):
  31. ????????media_type?=?"image"
  32. ????????upload_url?=?f"https://api.weixin.qq.com/cgi-bin/material/add_material?access_token={self.access_token}&type={media_type}"
  33. ????????with?open(image_path,?'rb')?as?file:
  34. ????????????response?=?requests.post(upload_url,?files={'media':?file})
  35. ????????#?解析响应
  36. ????????result?=?response.json()
  37. ????????#?print(result)
  38. ????????if?'media_id'?in?result:
  39. ????????????media_id?=?result['media_id']
  40. ????????????img_url?=?result['url']
  41. ????????????print(f"上传成功,media_id:?{media_id},?img_url:?{img_url}")
  42. ????????????return?media_id
  43. ????????else:
  44. ????????????print(f"上传失败,错误信息:?{result['errmsg']}")
  45. ????def?get_drafts(self,OFFSET=0):
  46. ????????"""
  47. ????????获取草稿列表
  48. ????????:param?OFFSET:
  49. ????????:return:?{
  50. ????"total_count":TOTAL_COUNT,
  51. ????"item_count":ITEM_COUNT,
  52. ????"item":[
  53. ????????"""
  54. ????????url?=?'https://api.weixin.qq.com/cgi-bin/draft/batchget?access_token={}'.format(self.access_token)
  55. ????????#?url?=?'https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=10&type=77&action=list_card&access_token={}&lang=zh_CN'.format(self.access_token)
  56. ????????data?=?{
  57. ????????????????"offset":OFFSET,
  58. ????????????????"count":20,
  59. ????????????????"no_content":0#1?表示不返回?content?字段,0?表示正常返回,默认为?0
  60. ????????????}
  61. ????????headers?=?{'Content-Type':?'application/json'}
  62. ????????response?=?requests.post(url,?data=json.dumps(data,?ensure_ascii=False).encode('utf-8'),?headers=headers)
  63. ????????rt_data?=?response.json()
  64. ????????item?=?rt_data['item']
  65. ????????print(item)
  66. ????????return?item
  67. ????def?push_draft(self,title,?content,?image_id,author=''):
  68. ????????"""
  69. ????????发布草稿
  70. ????????:param?title:
  71. ????????:param?content:
  72. ????????:param?image_id:
  73. ????????:return:
  74. ????????"""
  75. ????????url?=?'https://api.weixin.qq.com/cgi-bin/draft/add?access_token={}'.format(self.access_token)
  76. ????????data?=?{
  77. ????????????"articles":?[
  78. ????????????????{
  79. ????????????????????"title":?title,
  80. ????????????????????"content":?content,
  81. ????????????????????"thumb_media_id":?image_id,
  82. ????????????????????"author":?author,
  83. ????????????????????"show_cover_pic":?1,
  84. ????????????????????"need_open_comment":?1,
  85. ????????????????????"only_fans_can_comment":?1
  86. ????????????????}
  87. ????????????]
  88. ????????}
  89. ????????headers?=?{'Content-Type':?'application/json'}
  90. ????????response?=?requests.post(url,?data=json.dumps(data,?ensure_ascii=False).encode('utf-8'),?headers=headers)
  91. ????????rt_data?=?response.json()
  92. ????????print(rt_data)
  93. ????????tw_media_id?=?rt_data.get('media_id','')
  94. ????????return?tw_media_id
  95. ????def?push_qun(self,wz_media_id):
  96. ????????"""
  97. ????????群发文本消息,需要认证号
  98. ????????https://developers.weixin.qq.com/doc/offiaccount/Message_Management/Batch_Sends_and_Originality_Checks.html#2
  99. ????????:param?wz_media_id:
  100. ????????:return:
  101. ????????"""
  102. ????????url?=?'https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token={}'.format(self.access_token)
  103. ????????#?data_text?=?{
  104. ????????#????????????"filter":{
  105. ????????#???????????????"is_to_all":True,
  106. ????????#???????????????#?"tag_id":2
  107. ????????#????????????},
  108. ????????#????????????"text":{
  109. ????????#???????????????"content":"CONTENT"
  110. ????????#????????????},
  111. ????????#?????????????"msgtype":"text",
  112. ????????#?????????????#?"thumb_media_id":"dRHPM3p6Z4GjH_z03ZjUtm28vb0PmNpF1D7DAiH8fSaqUK41p95Ii90msY5INydR",
  113. ????????#?????????}
  114. ????????data_news?=?{
  115. ???????????????????"filter":{
  116. ??????????????????????"is_to_all":True,
  117. ??????????????????????#?"tag_id":2
  118. ???????????????????},
  119. ???????????????????"mpnews":{
  120. ??????????????????????"media_id":wz_media_id
  121. ??????????????????????#?"media_id":"dRHPM3p6Z4GjH_z03ZjUtiZdiWCPi8VMJZ5zFIBuPhikUbeK_b4ee1pemRuwOa1M"
  122. ???????????????????},
  123. ????????????????????"msgtype":"mpnews",
  124. ????????????????????"send_ignore_reprint":0
  125. ????????????????}
  126. ????????response?=?requests.post(url,?json=data_news)
  127. ????????fb_data?=?response.json()
  128. ????????print(fb_data)
  129. ????????msg?=?fb_data['errmsg']
  130. ????????return?msg
  131. ????def?get_medias(self,OFFSET=0):
  132. ????????"""
  133. ????????获取图片列表
  134. ????????:param?OFFSET:
  135. ????????:return:?{'item':?[{'media_id':?'dRHPM3p6Z4GjH_z03ZjUtm1INFasZ3klvBr80q52hq81fiCdFv8vQEPSrWAu2H84',?'name':
  136. ????????"""
  137. ????????url?=?'https://api.weixin.qq.com/cgi-bin/material/batchget_material?access_token={}'.format(self.access_token)
  138. ????????#?url?=?'https://mp.weixin.qq.com/cgi-bin/appmsg?begin=0&count=10&type=77&action=list_card&access_token={}&lang=zh_CN'.format(self.access_token)
  139. ????????data?=?{
  140. ????????????????"offset":OFFSET,
  141. ????????????????"count":20,
  142. ????????????????"type":'image',#素材的类型,图片(image)、视频(video)、语音?(voice)、图文(news)
  143. ????????????????"group_id":'3'#分类id?特殊的
  144. ????????????}
  145. ????????headers?=?{'Content-Type':?'application/json'}
  146. ????????response?=?requests.post(url,?data=json.dumps(data,?ensure_ascii=False).encode('utf-8'),?headers=headers)
  147. ????????rt_data?=?response.json()
  148. ????????item?=?rt_data['item']
  149. ????????print(item)
  150. ????????return?item

个人微信公众号自动发布文章

三,如何将草稿自动群发给粉丝

自动发布的草稿需要模拟手动群发给粉丝,这里需要使用到浏览器自动化方法。发布的草稿即时通知到模拟操作接口,打开对应的文章,点击发表》弹框确认发表》确认群发等等一些列确认,这里有一点需要特别注意:需要在设置与开发》安全中心》风险操作保护的详情中关闭群发消息保护,(默认是开启的每次群发都需要管理员微信扫码确认),关闭后才能自动群发。以下是在批量处理软件的基础上做的一个测试软件《微信公众号自动发文助手》

个人微信公众号自动发布文章

总的来说个人微信公众号未认证想要做到全自动群发文章还是比较复杂的,特别是模拟浏览器自动化操作,需要注意的事项也比较多如:等待弹框确认确认再确认等等,本想通过抓包接口直接发布,但是一堆莫名其妙的参数,相较而言模拟浏览器自动化操作还更简单了。以上是微信公众号自动发文的方法,可能相关的点:公众号流量主自动化爆文机器人,自动写作自动发布;微信公众号智能推送定时发送怎么实现;公众号自动生成文章;如何将编辑好的公众号图文定时群发

8月16日更新,最近使用上述方法先通过api直接发布草稿,然后通过浏览器自动化模拟手动发送运行了一段时间,但是出现了一个莫名奇妙的问题,草稿发送了,但是模拟发布有时能点击提交有时却发布不成功。这不稳定的状态就很让人无语了,可能是模拟点击得太快了,但是由于个人的文章需要卡着时间点即时发送出去,于是想着还是通过抓包来实现个人公众号文章自动发布了,主要方法如下:

首先,通过抓包实现post提交数据登录,获取到登录二维码图片,然后上传到手机,扫码确认。将成功返回的cookie信息保存着,下次再运行时直接先通过本地cookie登录。cookie的有限期还不知道有几天,看到时的运行情况。通过定时每天早上九点检测一次,如果失效就重新获取二维码登录。

其次,发布文章的接口通过抓包可以获取,虽然有些加密参数,但是直接忽略跳过也能发布成功。

最后,将api直接发布的草稿与抓包实现的发布文章接口对接,同时还需要抓包获取草稿列表,这个相对简单,把最近的草稿发送出去即可。

通过抓包直接post提交文章数据,来的更稳定快捷,虽然上述方法实现了自动化发布文章,但是公众号内容是个关键,没吸引的东西,没关注也是白搭。继续搞内容………………

9月16日更新,经过一段时间的运行,发现微信公众号cookie的有效期只有3天左右,无论是通过不间断的访问首页还是通过浏览器刷新等方式都不能让cookie长期存活,只能等掉线后重新扫码登录,感觉很是无语。本想做个一劳永逸的自动化处理,结果还是要经常扫码登录,看来还得想其他办法了。

 

  • 我的微信
  • 这是我的微信扫一扫
  • weinxin
  • 我的微信公众号
  • 我的微信公众号扫一扫
  • weinxin