python第三方库itchat

  1. 安装itchat
    pip install itchat

2.导包
import itchat

3.登陆操作
itchat.login() # 只是普通的登陆,并不具有缓存的功能
itchat.auto_login(hotReload=True) #会保留登陆状态,在短时间内不用重新登陆

  1. 给微信手机助手发消息
    itchat.send('hello',toUserName='filehelper')

5.给微信助手发文件
#itchat.send_file('/etc/passwd',toUserName='filehelper')

2.统计你的好友的男女比例

friends = itchat.get_friends()
#print(friends)
info = {}
for friend in friends[1:]:
if friend['Sex'] == 1:
info['male'] = info.get('male',0) + 1
elif friend['Sex'] == 2:
info['female'] = info.get('female',0) + 1
else:
info['other'] = info.get('other',0) + 1
print(info)

3.
import os
#在python中执行shell命令
#1.可以判断命令是否执行成功
#返回值是0 执行成功
#返回值不为0 执行不成功
print(os.system('ls'))
#res = os.system('hostnameeeee')
#print(res)

#2.用来保存命令的执行结果
res = os.popen('hostname').read()
print(res)

4.图灵机器人微信聊天

import itchat
import requests

def get_tuling_reponse(_info):
print(_info)
api_url = 'http://www.tuling123.com/openapi/api' 注册图灵提供的url
data = {
'key':'28a1d488a7fe47b5b637b750a6f3d66b', 注册图灵机器人中的key
'info':_info,
'userid':'haha'
}
#发送数据到指定的网址,获取网址返回的数据
res = requests.post(api_url,data).json()
#print(res,type(res))
#给用户返回的内容
print(res['text'])
return (res['text'])

#get_tuling_reponse('给我讲个笑话')
#get_tuling_reponse('不好笑')

#时刻监控好友发送的文本信息,并且给与一个回复br/>@itchat.msg_register(itchat.content.TEXT,isFriendChat=True)
def text_repky(msg):

获取好友发送的文本信息

#返回文本信息
content = msg['Content']
#将好友的消息发送给机器人去处理,处理的结果就是返回给好友的信息
returnContent = get_tuling_reponse(content)
return returnContent

itchat.auto_login()
itchat.run()

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章