您的位置:首页 > 编程语言 > Python开发

2018别样跨年|今晚让Python陪你,简单的机器人聊天

2017-12-31 21:54 204 查看
安装:
QQ:
pip install qqbot
微信:
pipi install itchat
1.选择

from qqbot import _bot as bot
bot.Login(['-q','QQ号码'])
friends= bot.List('buddy','xxx')
for friend in friends:
    print (friend.name)
    bot.SendTo(friend,'Hello%s~xxxxxxxx'%(friend.name))

首先我们登录SmartQQ:

from qqbot import _bot as botbot.Login(['-q', 'XXXX'])

其中,XXXX输入你自己的QQ号。如果第一次登录,会跳出来一个二维码让你扫,扫一下即可,之后你的账号信息会被保存下来,下次再登录就可以直接调用了。

然后我们选择好友:

friends = bot.List('buddy')

如果想随机抽取一部分,比如10个, 则:

friends = random.sample(friends,10)

如果想定点发送,则:

friends = bot.List('buddy', 'XXX')

XXX里面放好友的备注。

下面我们来给随机(或者假装随机,此处应该划重点)的好友发消息:

for friend in friends:
    print(friend.name)
    bot.SendTo(friend,'Hello%s~这是一条来自Python机器人的消息~恭喜你被机器人抽中成为幸运的十个人之一~'%(friend.name))

2.微信
微信的操作非常类似,不过账号信息只能保存一阵子,所以需要经常扫码,如果扫码过于频繁,就会登录不上了……别问我怎么知道的,我刚想给好久没有勾搭的女神发消息呢……就崩溃了……

首先还是登录:

import

itchatitchat.auto_login(True)

True参数是指保存一阵子登录信息,如果不加则不保存。

获取好友列表和随机抽取:

friends

=
itchat.get_friends()

随机抽取10位好友:

import random
friends = random.sample(friends, 10)

或者假装随机抽取好友:

friends = itchat.search_friends(name='XXX')

XXX 填上备注/微信号/昵称。

发送消息:

import time
for friend in friends:
    print(friend)
    itchat.send('%s你好~这是一条来自Python机器人的问候~我随机抽取了十个人发送了这条消息~'%(friend['DisplayName']), friend['UserName'])
    time.sleep(0.5)

为了防止发送过快被封,加了一个小小的暂停。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息