python基础项目,微信自动回复!

python基础项目,微信自动回复!

Python 的概念

1 创建机器人

到以下网址创建一个机器人,免费的:

 学习Python中有不明白推荐加入交流群
                号:516107834
                群里有志同道合的小伙伴,互帮互助,
                群里有不错的学习教程!

http://www.tuling123.com/

2 完整代码

import itchat
import requests
import re
# 抓取网页
def getHtmlText(url):
 try:
 r = requests.get(url,timeout=30)
 r.raise_for_status()
 r.encoding = r.apparent_encoding
 return r.text
 except:
 return ""
# 自动回复
# 封装好的装饰器,当接收到的消息是Text,即文字消息
@itchat.msg_register(['Text','Map', 'Card', 'Note', 'Sharing', 'Picture'])
def text_reply(msg):
 # 当消息不是由自己发出的时候
 if not msg['FromUserName'] == Name["输入你的微信名字"]:
 # 回复给好友
 url = "http://www.tuling123.com/openapi/api?key=输入你的密码&info="
 url = url + msg['Text']
 html = getHtmlText(url)
 message = re.findall(r'"text":".*?"',html)
 reply = eval(message[0].split(':')[1])
 return reply
if __name__ == '__main__':
 itchat.auto_login()
 # 获取自己的UserName
 friends = itchat.get_friends(update=True)[0:]
 Name = {}
 Nic = []
 User = []
 for i in range(len(friends)):
 Nic.append(friends[i]["NickName"])
 User.append(friends[i]["UserName"])
 for i in range(len(friends)):
 Name[Nic[i]] = User[i]
 itchat.run()

注意事项

1.复制apikey后面的一行密码替换到上面代码的“输入你的密码”处。

python基础项目,微信自动回复!

python基础项目,微信自动回复!

2.运行程序时会弹出一个二维码,用手机扫码登陆即可,这是网页版微信。然后每当别人发信息给你时就会自动回复了。

猜你喜欢

转载自blog.csdn.net/qq_41841569/article/details/83507344
今日推荐