python第三方库itchat

  1. 安装itchat
    pip install itchat

2.导包
import itchatpython

3.登录操做
itchat.login() # 只是普通的登录,并不具备缓存的功能
itchat.auto_login(hotReload=True) #会保留登录状态,在短期内不用从新登录shell

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

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

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

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)ide

4.图灵机器人微信聊天post

import itchat
import requestsurl

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'])3d

#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()

相关文章
相关标签/搜索