2.導包
import itchatpython
3.登錄操做
itchat.login() # 只是普通的登錄,並不具備緩存的功能
itchat.auto_login(hotReload=True) #會保留登錄狀態,在短期內不用從新登錄shell
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()