|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
各位大神:请帮忙检查一下程序哪里出了问题,总是报错!
实现目标:
1、给微信群组发信息
2、发送的信息存储在excel文件
3、每天不定时会在excel中添加信息
4、检测到有信息添加后,自动发送到指定的微信群
下面是我的编程,学了半个月,总是报错。求助
#!/usr/bin/env python3
#coding:utf-8
import itchat,xlrd,time,random
from itchat.content import *
from threading import Thread
def print_t(string):
# 记录日志到文件
time_s = time.strftime('%Y-%m-%d %H:%M:%S ',time.localtime(time.time()))
if show_log:
f = open('wechat.log','a+')
print('{0} {1}'.format(time_s, string), file = f)
def get_replay(index):
#求table_p表单中第index行,返回第1的内容
nrows = table_p.nrows
#nrows返回表单内的行数
if (index != 0 and index <= nrows - 1):
conf = table_p.row_values(index)
#conf是数组,内容为第index行中,各单元格内容
if conf :
return '@%s' % (conf[0])
else:
return False
def get_group(index):
#返回数组room,room包括table_g中第index行中,第2、3列群名
global all_rooms
nrows = table_g.nrows
#nrows求table_g表单的行数
if index != 0 and index <= nrows :
conf = table_g.row_values(index)
#conf是数组,内容为第index行中,各单元格内容
if len(all_rooms) == 0:
all_rooms = itchat.get_chatrooms(update=True)
room = []
for i in range(len(all_rooms)):
# print_t(all_rooms[i].NickName)
for n in range(1,len(conf)):
if str(conf[n]) == all_rooms[i].NickName:
room.insert(0,all_rooms[i])
break
return room
else:
return []
def replay_group(text):
for i in range(len(g_rooms)):
print_t('正在发送至群:'+g_rooms[i].NickName)
itchat.send(text, toUserName=g_rooms[i].UserName)
# 发一条随机延时2-10秒,群发容易被封号
time.sleep(random.randint(2,7))
itchat.send('群发结束,已退出群发状态',toUserName='filehelper')
group_reply = False
group_replying = False
def new_send():
while :
global rows
newrows = table_p.nrows
if newrows > rows:
for i in range (rows,newrows)
send_text = get_replay(i)
replay_group(send_text)
rows = rows + 1
time.sleep(2)
if __name__ == '__main__':
# 设置参数
auto_reply = True
group_reply = False
group_replying = False
show_log = True
# 配置表
workbook = xlrd.open_workbook(u'conf/reply.xls')
table_p = workbook.sheet_by_name(u'personal')
table_g = workbook.sheet_by_name(u'group')
#记录所有群组
all_rooms = []
#记录要转发消息的群组
g_rooms = []
g_rooms = get_group(1)
rows = table_p.nrows
for i in range(1,rows):
send_text=get_replay(i)
print(send_text)
replay_group(send_text)
time.sleep(random.randint(2,5))
thread=Thread(target=new_send)
thread.start()
#初始化关键字表-----------------------
#-------------------------------------
itchat.auto_login(hotReload=True)
itchat.run()
我这里说几个最基本问题啊,我修改了以下,能运行了最起码,里边运行出现什么错误我不知道:
代码:
def new_send():
while True:
global rows
newrows = table_p.nrows
if newrows > rows:
for i in range (rows,newrows):
send_text = get_replay(i)
replay_group(send_text)
rows = rows + 1
time.sleep(2)
一是:while 后边加条件啊,我加了个Ture,完全不知道你怎么跳出循环;
二是:for后边跟:啊,
三是:for没有:导致的缩进错了,Tab缩进问题。
|
|