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

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