w17603340671 2018-11-17 04:14 采纳率: 0%
浏览 1037

python获取好友信息运行不了,大神帮帮忙

 #!/usr/bin/python
# coding: utf-8
import itchat
import os
import pandas as pd
import matplotlib.pyplot as plot
from wordcloud import WordCloud
from pyecharts import Bar, Page
import jieba

import sys

import importlib
importlib.reload(sys)

# 获取所有的群聊列表
def getRoomList():
    roomslist = itchat.get_chatrooms()
    return roomslist


# 获取指定群聊的信息
def getRoomMsg(roomName):
    itchat.dump_login_status()
    myroom = itchat.search_chatrooms(name=roomName)
    return myroom


# 统计省份信息
def getProvinceCount(cityCount, countName):
    indexs = []
    counts = []
    for index in cityCount.index:
        indexs.append(index)
        counts.append(cityCount[index])
    page = Page()
    labels = [indexs]
    sizes = [counts]
    attr = indexs
    v1 = counts
    bar = Bar(countName)
    bar.add("地区分布", attr, v1, is_stack=True, is_label_show=True, is_datazoom_show=True,
            is_random=True)
    page.add(bar)
    bar.show_config()
    bar.render()


# 制作性别统计图
def getSexCount(sexs, countName):
    labels = [u'男', u'女', u'未知']
    sizes = [sexs['男'], sexs['女'], sexs['未知']]
    print
    sizes
    colors = ['red', 'yellow', 'blue', 'green']
    explode = (0, 0, 0)
    patches, l_text, p_text = plot.pie(sizes, explode=explode, labels=labels, colors=colors,
                                       labeldistance=1.1, autopct='%2.0f%%', shadow=False,
                                       startangle=90, pctdistance=0.6)
    for t in l_text:
        t.set_size = 30
    for t in p_text:
        t.set_size = 20
    plot.axis('equal')
    plot.legend(loc='upper left', bbox_to_anchor=(-0.1, 1))
    plot.rcParams['font.sans-serif'] = ['Microsoft YaHei']
    plot.rcParams['axes.unicode_minus'] = False
    plot.title(countName)
    plot.grid()
    plot.show()


# 制作词云
def makeWorldCount(userName):
    users = []
    for user in userName:
        users.append(user)
    users = ','.join(users)
    print
    users
    font = os.path.join(os.path.dirname(__file__), "SIMKAI.TTF")
    wordcloud = WordCloud(font_path=font, width=1800, height=800, min_font_size=4, max_font_size=80, margin=2).generate(
        users)
    plot.figure()
    plot.imshow(wordcloud, interpolation='bilinear')
    plot.axis("off")
    plot.show()


# 获取性别统计
def getSex(df_friends):
    sex = df_friends['Sex'].replace({1: '男', 2: '女', 0: '未知'})
    sexCount = sex.value_counts()
    return sexCount


# 男性个性签名统计
def analyMale(df_friends):
    signature = df_friends[df_friends.Sex == 1]['Signature']
    signature = signature.unique()
    signature = "".join(signature)
    wordlist_after_jieba = jieba.cut(signature, cut_all=True)
    wl_space_split = " ".join(wordlist_after_jieba)
    font = os.path.join(os.path.dirname(__file__), "SIMKAI.TTF")
    wordcloud = WordCloud(font_path=font, max_words=200, max_font_size=50, margin=2).generate(wl_space_split)
    plot.figure()
    plot.imshow(wordcloud, interpolation='bilinear')
    plot.axis("off")
    plot.show()


# 女性个性签名统计
def analyFemale(df_friends):
    signature = df_friends[df_friends.Sex == 2]['Signature']
    signature = signature.unique()
    signature = "".join(signature)
    wordlist_after_jieba = jieba.cut(signature, cut_all=True)
    wl_space_split = " ".join(wordlist_after_jieba)
    font = os.path.join(os.path.dirname(__file__), "SIMKAI.TTF")
    wordcloud = WordCloud(font_path=font, max_words=200, max_font_size=50, margin=2).generate(wl_space_split)
    plot.figure()
    plot.imshow(wordcloud, interpolation='bilinear')
    plot.axis("off")
    plot.show()


def main():
    itchat.auto_login(hotReload=True)  # 自动登陆
    roomMsg = getRoomMsg(u'')  # 获取指定群聊的信息
    gsq = itchat.update_chatroom(roomMsg[0]['UserName'], detailedMember=True)
    df_friends = pd.DataFrame(gsq['MemberList'])  # 取出其中的用户信息并转为dataframe
    # sexs = getSex(df_friends)   #获取性别统计
    # getSexCount(sexs,"公司性别统计图")    #制作性别统计图,第一个参数为性别统计的结果,第二个参数为该图的名称
    # city = df_friends['Province']   #取出省份信息
    # City_count = city.value_counts()[:15]
    # City_count = City_count[City_count.index != '']
    # getProvinceCount(City_count,"位置统计图")     #制作位置统计图,第一个参数为位置统计的结果,第二个参数为该图的名称
    # makeWorldCount(df_friends['NickName'])  #制作词云,传入用户昵称
    # makeWorldCount(signature)
    analyMale(df_friends)


if __name__ == '__main__':
    main()

D:\Python34\python.exe C:/Users/17603/Desktop/QUN.py
Please press confirm on your phone.
Log in time out, reloading QR code.
Please press confirm on your phone.
Loading the contact, this may take a little while.
Login successfully as 王豆豆~
Building prefix dict from the default dictionary ...
Loading model from cache C:\Users\17603\AppData\Local\Temp\jieba.cache
Loading model cost 1.084 seconds.
Prefix dict has been built succesfully.
Traceback (most recent call last):
  File "C:/Users/17603/Desktop/QUN.py", line 144, in <module>
    main()
  File "C:/Users/17603/Desktop/QUN.py", line 140, in main
    analyMale(df_friends)
  File "C:/Users/17603/Desktop/QUN.py", line 105, in analyMale
    wordcloud = WordCloud(font_path=font, max_words=200, max_font_size=50, margin=2).generate(wl_space_split)
  File "D:\Python34\lib\site-packages\wordcloud\wordcloud.py", line 605, in generate
    return self.generate_from_text(text)
  File "D:\Python34\lib\site-packages\wordcloud\wordcloud.py", line 587, in generate_from_text
    self.generate_from_frequencies(words)
  File "D:\Python34\lib\site-packages\wordcloud\wordcloud.py", line 473, in generate_from_frequencies
    font = ImageFont.truetype(self.font_path, font_size)
  File "D:\Python34\lib\site-packages\PIL\ImageFont.py", line 280, in truetype
    return FreeTypeFont(font, size, index, encoding, layout_engine)
  File "D:\Python34\lib\site-packages\PIL\ImageFont.py", line 145, in __init__
    layout_engine=layout_engine)
OSError: cannot open resource

Process finished with exit code 1


上面是代码,下面是错误信息

  • 写回答

1条回答

  • devmiao 2018-11-17 13:20
    关注
    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮