python---微信

import itchat

itchat。auto_login(hotReload=True)#登入微信

friend=itchat。search_friends(nickName=“ge”)[0][“UserName”]#確定用哪個微信傳送訊息

@itchat。msg_register(itchat。content。TEXT)#根據不同命令執行不同功能

def info_handle(msg):

#列印訊息

print(msg[“Content”])

#分析訊息

if msg[“Content”]==“拍照”:

#獲取攝像頭0編號

capture=cv2。VideoCapture(0)

#有沒有成功獲取照片,資料

frame=capture。read()[1]

#圖片

cv2。imwrite(“photo。png”,frame)

itchat。run()

獲取微信好友頭像

import itchat

itchat。auto_login()#登入

#獲取好友列表

for friend in itchat。get_friends():

img=itchat。get_head_img(userName=friend[“UserName”]

#定義路徑,儲存位置

path=“ ____ ”+“friend[”NickName“]+”。jpg“

print(”正在下載%s的頭像“%friend[”NickName“])

with open(path,”wb“) as f:

f。write(img)

itchat。run()

求微信好友男女比例

import itchat

#自動登入

itchat。auto_login(hotReload=True)

male=female=other=0

#獲取微信好友列表

friends= itchat。get_friends()

for friend in friends[1:]:

sex=friend[”Sex“]

if sex==1:

male+=1

elif sex==2:

female+=1

else:

other+=1

#求好友總數

total=len[friends]

print(total)

#男女比例

print("male:%。2f"%%%(float(male)/total*100))

微信自動回覆 圖靈介面

import itchat

import requests

itchat。auto_login(hotReload=True)

apiurl=”http://www。tuling。com/openapi/api“

def get_message(message):

#接收微信好友傳送的訊息,根據訊息回覆

data={

”key“:” ____“

”userId“:”robot“

”info“:”message“

try:

r=requests。post(apiurl,data=data)。json()

print(”robot的回覆%s“%r[”text“])

return r[”text“]

except :

return ” “

#裝飾器,給函式增加功能

@itchat。msg_register(itchat。content。TEXT)

def auto_reply(msg):

defalutreplay=”好的“

friend=itchat。search_friends(”ge“)

#獲取微信好友姓名

readFriendUserName=friend[0][”UserName“]

print(”message“:%s”%msg[“Text”])

reply=get_message(msg[“Text”])

if msg[“FromUserName”]==readFriendUserName:

itchat。send(reply or defalutreply,toUserName=readFriendUserName)

itchat。run()