itchat怎样监听指定微信群的消息??下面我自己写的代码,虽然大部分都监听到这个群的消息,但还监听了别的群的消息,哪里搞错了,请高人指点??多谢
[Python] 纯文本查看 复制代码 import itchat
from itchat.content import TEXT
import pdb
import re
import webbrowser
@itchat.msg_register(TEXT, isGroupChat=True) #这里的TEXT表示如果有人发送文本消息,那么就会调用下面的方法
def simple_reply(msg):
rooms = itchat.search_chatrooms(name='京东股东群') #找到群名
#print(rooms[0]['UserName'])
if len(rooms)!=0:
message = msg['Content']
dic = ['https://']
#for i in range(len(dic)):
if dic[0] in message: #本以为需要结巴分词,没想到可以直接查找
print('--------------------------------------')
print(message)
#itchat.send(message, 'filehelper') #发送给我的手机文件助手
pattern = re.compile(r'https?://(?:[a-zA-Z]|[0-9]|[$-_@.&+]|[!*\(\),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+')
urls = re.findall(pattern, message)
if len(urls) != 0:
for i in range(len(urls)):
print(urls[i])
webbrowser.open(urls[i])
#webbrowser.open(url)
itchat.auto_login(hotReload = True) #hotReload = True 不用重复扫描二维码
itchat.run()
|