zoukankan      html  css  js  c++  java
  • python微信好友数据分析与聊天机器人

    一、微信好友数据分析代码

    from wxpy import *      #导入函数
    bot = Bot(cache_path=True)
    friend_all = bot.friends()
    print(friend_all[0].raw)
    len(friend_all)
    lis=[]
    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)
    def lis2e07(filename,lis):#把数据写入xlsx中
        import openpyxl
        wb = openpyxl.Workbook()
        sheet = wb.active
        sheet.title = 'list2excel07'
        file_name = filename +'.xlsx'
        for i in range(0, len(lis)):
            for j in range(0, len(lis[i])):
                sheet.cell(row=i+1, column=j+1, value=str(lis[i][j]))
        wb.save(filename)
        print("写入数据成功!")
    lis2e07('yubg1',lis)#开始运行函数
    Friends = bot.friends()
    data = Friends.stats_text(total=True, sex=True,top_provinces=30, top_cities=500)
    print(data)#输出好友数据
    from pandas import read_excel#获取好友所在地
    df = read_excel('yubg1.xlsx',sheetname='list2excel07')
    df.tail(5)
    df.city.count()
    df.city.describe()
    from wordcloud import WordCloud#创建好友所在地的词云
    import matplotlib.pyplot as plt
    import pandas as pd
    from pandas import DataFrame
    word_list= df['city'].fillna('0').tolist()
    new_text = ' '.join(word_list)
    wordcloud = WordCloud(font_path='simhei.ttf',  background_color="black").generate(new_text)
    plt.imshow(wordcloud)
    plt.axis("off")
    plt.show()
    province_list = df['province'].fillna('NAN').tolist()
    count_province = pd.value_counts(province_list)
    from pyecharts import Map#创建中国地图,并把好友所在地的位置标明出来
    value =count_province.tolist()
    attr =count_province.index.tolist()
    map=Map("各省微信好友分布", width=1200, height=600)
    map.add("", attr, value, maptype='china', is_visualmap=True,visual_text_color='#000',is_label_show = True)
    map.show_config()
    map.render(r'c:UsersJ&Pmap1.html') 

    二、代码详解

    代码运行时,会出现一个二维码,那是用来登录网页微信的,可以扫,但要记得去手机上结束登陆,这样代码就会结束运行了。

    然后,是代码运行展示,最后,会展示出一个中国地图

    如图所示

    三、聊天机器人代码

    1、首先要去图灵官网,去建立一个聊天机器人,然后,就可以让自己的微信变为一个聊天机器人了

    代码详情如下,要得到api账号和密码。

    mport itchat
    import requests
    
    def get_response(msg):
        apiurl = 'http://i.itpk.cn/api.php'  #moli机器人的网址
        data={
            "question": msg,   #获取到聊天的文本信息
            "api_key": "f99135ba6a4946da9625234c2ec6a4f0",
            "api_secret": "0098abb38bca0298 "
        }
    
        r=requests.post(apiurl,data=data)  #构造网络请求
        return r.text
    @itchat.msg_register([itchat.content.TEXT], isGroupChat=True)     #好友消息的处理
    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()                       #启动聊天机器人

    然后,你的账号就是编程聊天机器人了,不过小心别打扰到别人昂。

  • 相关阅读:
    软件工程实践2019第三次作业
    软件工程实践2019第二次作业
    软件工程实践2019第一次作业
    《暗时间》读书笔记
    2020 软工实践 寒假作业(1/2)
    个人技术博客——Spring中Controller的传参与返回值处理
    2020 软工实践 个人作业——软件工程实践总结
    2020 软工实践 个人作业——软件测评
    2020 软工实践 疫情统计可视化 (实现)
    2020 软工实践 疫情统计可视化(原型设计)
  • 原文地址:https://www.cnblogs.com/asd516970982/p/10982674.html
Copyright © 2011-2022 走看看