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个回答

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐