1. 程式人生 > 實用技巧 >Python微信機器人

Python微信機器人

本文目錄

回到目錄

一 簡介

wxpy基於itchat,使用了 Web 微信的通訊協議,,通過大量介面優化提升了模組的易用性,並進行豐富的功能擴充套件。實現了微信登入、收發訊息、搜尋好友、資料統計等功能。

總而言之,可用來實現各種微信個人號的自動化操作。(http://wxpy.readthedocs.io/zh/latest/bot.html)

安裝:wxpy 支援 Python 3.4-3.6,以及 2.7 版本

pip3 install -U wxpy

安裝 pillow模組

pip3 install pillow

安裝 pyecharts模組

pip3 install pyecharts

回到目錄

二 登入微信

1 、 掃碼登入微信

from wxpy import *

bot = Bot()

2、cache_path=True

執行上面的程式,會彈出二維碼,用手機微信掃一掃即可實現登入。

但上面的程式有一個缺點,每次執行都要掃二維碼。不過wxpy非常貼心地提供了快取的選項,用於將登入資訊儲存下來,就不用每次都掃二維碼,如下

bot = Bot(cache_path=True) # 必須先登入過一次以後才可以使用快取
回到目錄

三 微信好友男女比例

from wxpy import *
from pyecharts import
Pie import webbrowser bot=Bot(cache_path=True) #注意手機確認登入 friends=bot.friends() #拿到所有朋友物件,放到列表裡 attr=['男朋友','女朋友','未知性別'] value=[0,0,0] for friend in friends: if friend.sex == 1: # 等於1代表男性 value[0]+=1 elif friend.sex == 2: #等於2代表女性 value[1]+=1 else: value[2]+=1

pie = Pie("

朋友男女比例")
pie.add(
"", attr, value, is_label_show=True)
#圖表名稱str,屬性名稱list,屬性所對應的值list,is_label_show是否現在標籤
pie.render('sex.html')#生成html頁面

開啟瀏覽器

webbrowser.open("sex.html")

回到目錄

四 微信好友地域分佈

顯示中國地圖,需要裝中國地圖模組:

全球國家地圖: echarts-countries-pypkg (1.9MB): 世界地圖和 213 個國家,包括中國地圖
中國省級地圖: echarts-china-provinces-pypkg (730KB):23 個省,5 個自治區
中國市級地圖: echarts-china-cities-pypkg (3.8MB):370 箇中國城市
中國縣區級地圖: echarts-china-counties-pypkg (4.1MB):2882 箇中國縣·區
中國區域地圖: echarts-china-misc-pypkg (148KB):11 箇中國區域地圖,比如華南、華北。

特別註明,中國地圖在 echarts-countries-pypkg 裡。需要這些地圖的朋友,可以裝 pip 命令列:

$ pip3 install echarts-countries-pypkg
$ pip3 install echarts-china-provinces-pypkg
$ pip3 install echarts-china-cities-pypkg
$ pip3 install echarts-china-counties-pypkg
$ pip3 install echarts-china-misc-pypkg

from wxpy import *
from pyecharts import Map
import webbrowser
bot=Bot(cache_path=True)

friends=bot.friends()

area_dic={}#定義一個字典,用來存放省市以及省市人數
for friend in friends:
if friend.province not in area_dic:
area_dic[friend.province]
=1
else:
area_dic[friend.province]
+=1

attr = area_dic.keys()
value
= area_dic.values()

map = Map("好朋友們的地域分佈", width=1200, height=600)
map.add(
"好友地域分佈",
attr,
value,
maptype
='china',
is_visualmap
=True, #結合體VisualMap

)
#is_visualmap -> bool 是否使用視覺對映元件

map.render('area.html')

webbrowser.open("area.html")

回到目錄

五 微信聊天機器人

1、為微信傳輸助手傳送訊息

這裡的file_helper就是微信的檔案傳輸助手,我們給檔案傳輸助手傳送一條訊息,可以在手機端的檔案傳輸助手中收到括號內的訊息

bot.file_helper.send('lqz say hello')

2、收發訊息@bot.register()

from wxpy import *
bot=Bot(cache_path=True)

@bot.register()
def recv_send_msg(recv_msg):
print('收到的訊息:',recv_msg.text) # recv_msg.text取得文字
return '自動回覆:%s' %recv_msg.text

# 進入Python命令列,讓程式保持執行
embed()

3、自動給老婆回覆資訊

當你在網咖吃著雞,操作騷出天際時,你老婆打電話讓你回家吃飯,此時你怎麼辦。。。

from wxpy import *
bot=Bot(cache_path=True)

girl_friend=bot.search('劉劉劉')[0]
print(girl_friend)

@bot.register() # 接收從指定好友發來的訊息,傳送者即recv_msg.sender為指定好友girl_friend
def recv_send_msg(recv_msg):
print('收到的訊息:',recv_msg.text) # recv_msg.text取得文字
if recv_msg.sender == girl_friend:
recv_msg.forward(bot.file_helper,prefix
='老婆留言: ') #在檔案傳輸助手裡留一份,方便自己忙完了回頭檢視
ms='老婆最美麗,我對老婆的愛如滔滔江水,連綿不絕'
print('>>>給老婆回覆的:', ms)
return ms#給老婆回一份

embed()

4、從微信群裡定位好友之拍老闆馬屁

from wxpy import *
bot=Bot(cache_path=True)

company_group=bot.groups().search('群名字')[0]

boss=company_group.search('老闆名字')[0]

@bot.register(chats=company_group) #接收從指定群發來的訊息,傳送者即recv_msg.sender為組
def recv_send_msg(recv_msg):
print('收到的訊息:',recv_msg.text)
if recv_msg.member == boss:
#這裡不用recv_msg.render 因為render是群的名字
recv_msg.forward(bot.file_helper,prefix='老闆發言: ')
return '老闆說的好有道理,深受啟發'

embed()

5、聊天機器人

給所有人自動回覆

import json
import requests
from wxpy import *
bot = Bot(cache_path=True)

# 呼叫圖靈機器人API,傳送訊息並獲得機器人的回覆
def auto_reply(text):
url
= "http://www.tuling123.com/openapi/api"
api_key
= "9df516a74fc443769b233b01e8536a42"
payload
= {
"key": api_key,
"info": text,
}
r
= requests.post(url, data=json.dumps(payload))
result
= json.loads(r.content)
return "[來自智慧機器人] " + result["text"]

@bot.register()
def forward_message(msg):
return auto_reply(msg.text)

embed()

給指定的群回覆

import json
import requests
from wxpy import *
bot = Bot(cache_path=False)

group=bot.groups().search('群名字')[0]
print(group)

# 呼叫圖靈機器人API,傳送訊息並獲得機器人的回覆
def auto_reply(text):
url
= "http://www.tuling123.com/openapi/api"
api_key
= "9d602fe417464cd18beb2083d064bee6"
payload
= {
"key": api_key,
"info": text,
}
r
= requests.post(url, data=json.dumps(payload))
result
= json.loads(r.content)
return "[來自智慧機器人] " + result["text"]

@bot.register(chats=group)
def forward_message(msg):
return auto_reply(msg.text)

embed()

給指定的人回覆

import requests
from wxpy import *
bot = Bot( cache_path=True)

girl_friend=bot.search('名字r')[0]

# 呼叫圖靈機器人API,傳送訊息並獲得機器人的回覆
def auto_reply(text):
url
= "http://www.tuling123.com/openapi/api"
api_key
= "申請圖靈機器人獲取key值放到這裡"
payload
= {
"key": api_key,
"info": text,
}
r
= requests.post(url, data=json.dumps(payload))
result
= json.loads(r.content)
return "[微信測試,請忽略] " + result["text"]

@bot.register()
def forward_message(msg):
if msg.sender == girl_friend:
return auto_reply(msg.text)

embed()

[轉載至原文連結](https://www.cnblogs.com/liuqingzheng/articles/9079192.html)