您的位置:首页 > 编程语言 > Python开发

Python查看微信好友男女比例及地域为位置和图灵机器人自动聊天

2020-02-17 05:08 856 查看

首先在pycharm中
安装:wxpy 支持 Python 3.4-3.6,以及 2.7 版本

pip install -U wxpy

安装 pillow模块

pip install pillow

安装 pyecharts模块

pip install pyecharts

安装 pyecharts_snapshot 模块

pip install pyecharts_snapshot

安装 echarts-countries-pypkg 模块

pip3 install echarts-countries-pypkg

然后添加代码
这是查看男女比例的

from wxpy import *
from pyecharts import Pie
import webbrowser
bot=Bot() #注意手机确认登录

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

这是看地域位置的

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

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

然后是图灵机器人聊天的

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

# 调用图灵机器人API,发送消息并获得机器人的回复
def auto_reply(text):
url = "http://www.tuling123.com/openapi/api"
api_key = "9df516a74fc443769b233b01eqwer42"
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()

附上一个对方发什么消息就回什么消息的

from wxpy import *
bot=Bot()

@bot.register()
def recv_send_msg(recv_msg):
print('收到的消息:',recv_msg.text) # recv_msg.text取得文本
return '自动回复:%s' %recv_msg.text

# 进入Python命令行,让程序保持运行
embed()
  • 点赞
  • 收藏
  • 分享
  • 文章举报
weixin_44273313 发布了3 篇原创文章 · 获赞 0 · 访问量 255 私信 关注
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: