热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

python微信群发_更新新年祝福群发!小白转战Python微信定时发送消息给群或是个人!...

本帖最后由新手小白学编程于2020-1-2421:07编辑{:301_998:}过年了发点祝福短信发现人太多了,老代码改一改直接定时群发走起![Py

本帖最后由 新手小白学编程 于 2020-1-24 21:07 编辑

{:301_998:} 过年了发点祝福短信发现人太多了,老代码改一改直接定时群发走起!

[Python] 纯文本查看 import itchat,time

import sys

from itchat.content import *

import datetime

import time

itchat.auto_login(hotReload=True)

wish_list =['用一对明亮的眼发现鼠年的美好,\n用一双勤劳的手创造鼠年的财富,\n用一颗宽容的心迎接鼠年的快乐,\n用一个灿烂的梦温暖鼠年的风景。\n鼠年快乐!']

#wish_list = ['除夕之夜很特别,一夜之间跨两年。\n家家户户守岁忙,灯火通明灯不灭。\n父母妻儿聚一起,共同等候新年来。\n朋友之情怎能忘,手指不停祝福传。\n祝你阖家幸福,生活美满。\n除夕快乐!']

SINCERE_WISH = wish_list[0]

friendList = itchat.get_friends(update=True)[1:]

while 1:

now = datetime.datetime.now()

now_str = now.strftime('%Y-%m-%d %H:%M:%S')[:10] #获取时间以便于做定时使用

now_tm = now.strftime('%Y-%m-%d %H:%M:%S')[:19]

#print(now_str)

print('\r{}'.format(now_tm), end ='')

if now_str in ['2020-01-25']:

for g in range(0,len(friendList)):

itchat.send(SINCERE_WISH,friendList[g]['UserName'])

print(now_tm)#打印发送的时间

print( (friendList[g]['RemarkName'] or friendList[g]['NickName']),'已发送')

sys.stdout.write(str(g+1)+"/"+str(len(friendList))+"\r")

sys.stdout.flush()

time.sleep(2)

print('done')

time.sleep(1) # 1秒发送三次 # 2秒发送四次

if __name__ == '__main__':

itchar.auto_login()

itchar.run()

{:301_972:}因为需要群发消息给所有微信好友,自己改了个代码发现不行,换了个思路完美解决现在把代码给大家发出来,有需要自己带走

{:301_999:}工作需要每天要推送消息到某些群,还要给大家分别推送消息就专门去找了这些代码,并进行了一些修改,如果有其他需要大家自行补充,另外就是免责声明了:纯属个人学习成品,觉得不实用或是有盗版嫌疑的,请大家勿怪,我在论坛上没找到相关内容,这是我补充的,新手小白勿怪!反正是学习的内容,我把整个程序都贴上去了,想要使用的自己复制粘贴!{:301_986:}使用本本程序前请自行查阅自己的微信是否支持网页版的登录,如不支持无法使用;

使用本程序前请安装Python环境;

使用本程序前请安装Ithat环境或wxpy环境;

如果不会安装环境请自行百度PIP install Itchat 与 PIP install wxpy;

微信群跟微信单人发送的区别已经标出来了,各位大佬自行修改就行!

user =itchat.search_chatrooms(name=u'微信群名')

user =itchat.search_friends(name=u'名字') #修改名字单人

{:301_1001:}不说了上程序上图{:301_997:}更新代码:群发好友

[Python] 纯文本查看 import itchat,time

import sys

from itchat.content import *

itchat.auto_login(hotReload=True)

wish_list = ['微信僵尸粉测试代码请勿回复']

SINCERE_WISH = wish_list[0]

friendList = itchat.get_friends(update=True)[1:]

for g in range(0,len(friendList)):

itchat.send(SINCERE_WISH,friendList[g]['UserName'])

print((friendList[g]['RemarkName'] or friendList[g]['NickName']),'已发送')

sys.stdout.write(str(g+1)+"/"+str(len(friendList))+"\r")

sys.stdout.flush()

time.sleep(2)

print('done')

以下是老代码了

{:301_1000:}开图:

none.gif

QQ图片20190611204607.png (20.5 KB, 下载次数: 1)

登陆成功

2019-6-11 20:47 上传

none.gif

QQ图片20190611204706.png (25.37 KB, 下载次数: 1)

当天提醒全部完成

2019-6-11 20:47 上传

{:301_978:}上程序

[Python] 纯文本查看 import itchat

import datetime

import time

itchat.auto_login(hotReload=True)#保留登录历史下次直接手机登录

user =itchat.search_friends(name=u'名字') #修改名字就行 U可删可不删

userName = user[0][u'UserName']#提取出需要的名字对应的编号

while 1:

now = datetime.datetime.now()#取时间

now_str =now.strftime('%Y-%m-%d %H:%M:%S')[11:]#取时间11位往后的

# print(now_str)

print('\r{}'.format(now_str), end ='')#打印时间

if now_str in ['15:33:00']:#定时任务开始执行

itchat.send('早上好!起床吃早餐了!',toUserName=userName)

time.sleep(1)

if now_str in ['12:00:00']:

itchat.send('该吃午饭了!',toUserName=userName)

time.sleep(1)

if now_str in ['18:00:00']:

itchat.send('该吃晚饭了!',toUserName=userName)

time.sleep(1)

if now_str in ['21:00:00']:

itchat.send('晚安!',toUserName=userName)

print('今天的提醒已全部发送完了!')#当天最后一个任务完成后的提醒

time.sleep(1)

time.sleep(1) # 1秒发送三次 # 2秒发送四次

if __name__ == '__main__':

itchar.auto_login()#扫码登录

itchar.run()#保持微信登录状态

{:301_1001:}第二套程序针对返回微信群的自行修改群名即可//要求微信群必须在通讯录内否则无法发送,具体方式请自行百度

[Python] 纯文本查看 import itchat

import datetime

import time

itchat.auto_login(hotReload=True)

user =itchat.search_chatrooms(name=u'微信群名')

userName = user[0][u'UserName']

while 1:

now = datetime.datetime.now()

now_str =now.strftime('%Y-%m-%d %H:%M:%S')[11:]

# print(now_str)

print('\r{}'.format(now_str), end ='')

if now_str in ['07:50:00']:

itchat.send('所要发送的内容',toUserName=userName)

time.sleep(1)

if now_str in ['20:00:00']:

itchat.send('所要发送的内容',toUserName=userName)

time.sleep(1)

print('今天提醒已发送成功')

time.sleep(1) # 1秒发送三次 # 2秒发送四次

if __name__ == '__main__':

itchar.auto_login()

itchar.run()

{:301_980:}忘记加wxpy环境的了现在补上

[Python] 纯文本查看 from __future__ import unicode_literals

from threading import Timer

from wxpy import *

import requests

import itchat

bot = None

def get_news():

#获取一个连接中的内容

url = "http://open.iciba.com/dsapi/"

r = requests.get(url)

# print(r.json())

contents = r.json()['content']

translation = r.json()['translation']

return contents,translation

def login_wechat():

global bot

bot = Bot( cache_path=True)

# bot = Bot(console_qr=2,cache_path="botoo.pkl")#linux环境上使用

def send_news():

if bot == None:

login_wechat()

try:

my_friend = bot.friends().search(u'微信昵称')[0] #xxx表示微信昵称

my_friend.send(get_news()[0])

my_friend.send(get_news()[1][5:])

my_friend.send(u"来自最爱你的人")

print(u"成功!!")

t = Timer(86400, send_news) #360是秒数

t.start()

except:

print(u"失败!!")

if __name__ == "__main__":

send_news()

#print(get_news()[0])

{:301_973:}最后这个在论坛上可能有具体哪个位置我也忘了,希望兄弟不要介意,我也是刚学,还有的不太懂

[Python] 纯文本查看 import itchat,time

import sys

from itchat.content import *

import datetime

import time

itchat.auto_login(hotReload=True)

wish_list =['用一对明亮的眼发现鼠年的美好,\n用一双勤劳的手创造鼠年的财富,\n用一颗宽容的心迎接鼠年的快乐,\n用一个灿烂的梦温暖鼠年的风景。\n鼠年快乐!']

#wish_list = ['除夕之夜很特别,一夜之间跨两年。\n家家户户守岁忙,灯火通明灯不灭。\n父母妻儿聚一起,共同等候新年来。\n朋友之情怎能忘,手指不停祝福传。\n祝你阖家幸福,生活美满。\n除夕快乐!']

SINCERE_WISH = wish_list[0]

friendList = itchat.get_friends(update=True)[1:]

while 1:

now = datetime.datetime.now()

now_str = now.strftime('%Y-%m-%d %H:%M:%S')[:10] #获取时间以便于做定时使用

now_tm = now.strftime('%Y-%m-%d %H:%M:%S')[:19]

#print(now_str)

print('\r{}'.format(now_tm), end ='')

if now_str in ['2020-01-25']:

for g in range(0,len(friendList)):

itchat.send(SINCERE_WISH,friendList[g]['UserName'])

print(now_tm)#打印发送的时间

print( (friendList[g]['RemarkName'] or friendList[g]['NickName']),'已发送')

sys.stdout.write(str(g+1)+"/"+str(len(friendList))+"\r")

sys.stdout.flush()

time.sleep(2)

print('done')

time.sleep(1) # 1秒发送三次 # 2秒发送四次

if __name__ == '__main__':

itchar.auto_login()

itchar.run()



推荐阅读
author-avatar
大爱河曲Q网
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有