利用python实现微信小项目

我们的好友都来自五湖四海,我们可以利用python的强大的数据库来实现对好友地域的统计

工具:wxpy库,wordcloud库,matplotlib库,openpyxl库,pandas库,numpy库。

1、首先实现对微信的登陆与获取好友数据操作

  代码:

如果你对python感兴趣,我这有个学习Python基地,里面有很多学习资料,感兴趣的+Q群:688244617

#引入微信登陆接口
from wxpy import *

#获取登录二维码
bot = Bot(cache_path = True)

#获取微信朋友的基本数据
friend_all = bot.friends()

#建立一个二维列表,存储基本好友信息
lis = [['NickName','Sex','City','Province','Signature','HeadImgUrl','HeadImgFlag']]
#把好有特征数据保存为列表
for a_friend in friend_all:
    NickName = a_friend.raw.get('NickName', None)
    Sex = {1:"男", 2:"女", 0:"其它"}.get(a_friend.raw.get('Sex', None), None)
    City = a_friend.raw.get('City', None)
    Province = a_friend.raw.get('Province', None)
    Signature = a_friend.raw.get('Signature', None)
    HeadImgUrl = a_friend.raw.get('HeadImgUrl', None)
    HeadImgFlag = a_friend.raw.get('HeadImgFlag', None)
    list_0 = [NickName, Sex, City, Province, Signature, HeadImgUrl, HeadImgFlag]
    lis.append(list_0)
  运行结果:

在这里插入图片描述

此时弹出了二维码,扫码登陆即可自动获取好友数据

2、将好友数据保存为xlsx文件

 方便worldcloud库进行处理

代码:

#把列表转换为 xlsx 表格
def list_to_xlsx(filename, list):
    
    import openpyxl
    wb = openpyxl.Workbook()
    sheet = wb.active
    sheet.title = 'Friends'
    file_name = filename + '.xlsx'
    for i in range(0, len(list)):
        for j in range(0, len(list[i])):
            sheet.cell(row = i+1, column = j+1, value = str(list[i][j]))
            
    wb.save(file_name)
    print("读写数据成功")

#把列表生成表格
list_to_xlsx('wechat_friend', lis)
    运行结果:

在这里插入图片描述

生成了一个xlsx文件

3、生成词云

  利用worldcloud库生成一个词云

代码:

from wordcloud import WordCloud
import matplotlib.pyplot as plt
import pandas as pd
from pandas import read_excel
import numpy as np

df = read_excel('wechat_friend.xlsx')

#使用 WordCloud 生成词云
word_list = df['City'].fillna('0').tolist()
new_text = ' '.join(word_list)
wordcloud = WordCloud(font_path='msyh.ttc', background_color = 'white').generate(new_text)
plt.imshow(wordcloud)
plt.axis("off")
plt.show()


#使用 pyecharts 生成词云
from pyecharts import WordCloud

city_list = df['City'].fillna('').tolist()
count_city = pd.value_counts(city_list)
name = count_city.index.tolist()
value = count_city.tolist()

wordcloud = WordCloud(width=1300,height=620)
wordcloud.add("",name,value,word_size_range=[20,100])
#wordcloud.show_config()
#wordcloud.render(r'D:\wc.html')
wordcloud.render('wordcloud.html')


#将好友展示在地图上 
from pyecharts import Map

province
  运行结果:

在这里插入图片描述

二、创建微信机器人
工具:requests模块,itchat模块

然后我们去茉莉机器人上申请api接口http://www.itpk.cn

代码:

#-*- coding:utf-8 -*-
import itchat
import requests

def get_response(msg):
    apiurl = 'http://i.itpk.cn/api.php'  //moli机器人的网址
    data={
        "question": msg,    //获取到聊天的文本信息
        "api_key": "9ddf52cacd0ef429d1c63bf411b9bed6",
        "api_secret": "n4gxkdyckd7p"
    }
 
    r=requests.post(apiurl,data=data)  //构造网络请求
    return r.text
@itchat.msg_register(itchat.content.TEXT)     //好友消息的处理
def print_content(msg):
    return get_response(msg['Text'])
@itchat.msg_register([itchat.content.TEXT], isGroupChat=True)    //群消息的处理
def print_content(msg):
    return get_response(msg['Text'])
itchat.auto_login(True)           //自动登录
itchat.run()                       //启动聊天机器人

运行登陆即可

  • 1
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值