Micro-channel small projects

 Analysis of micro-letter friends

# - * - Coding: UTF-. 8 - * - 
# Import module 
from wxpy Import *
 Import openpyxl
 Import PANDAS AS PD
 from pyecharts Import the Map
 from pyecharts Import wordcloud
 # initialization robot, selection cache mode (scan code) log 
bot = Bot (cache_path = True)
 # get my friends all the micro-channel information 
friend_all = bot.friends ()
 # Print (friend_all [0] .raw) friend_all [0] is my micro-letter nickname, .raw is to obtain all the information my 
# obtain micro-channel buddy list information into 
L = []
 for a_friend in friend_all:
    NickName=a_friend.raw.get('NickName',None)
    Sex=a_friend.raw.get('Sex',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]
    l.append(list_0)
# Coarse Acquisition friend of statistical information 
the Data = friend_all.stats_text (Total = True, Sex = True, top_provinces = 30, top_cities = 500 )
 # -defined functions, stored in the list l xlsx file named filename 
DEF lte (filename, l) :
    wb=openpyxl.Workbook()
    sheet=wb.active
    sheet.title='lte'
    file_name=filename+'.xlsx'
    for i in range(0,len(l)):
        for j in range(0,len(l[i])):
            sheet.cell(row=i+1,column=j+1,value=str(l[i][j]))
    wb.save(file_name)
# -Defined functions to HTML file into a word cloud 
DEF WTH ():
    df=pd.read_excel('text.xlsx')
    COUNT = df.city.value_counts () # of dataframe full frequency statistics, excluding NaN3 
    city_list DF = [ ' City ' ] .fillna ( ' from NAN ' ) .ToList () # the column into List dataframe, wherein nan replaced with "NAN" 
    count_city = pd.value_counts (city_list) # of full-frequency statistics 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 ( ' wcl.html ' )
 # These friends do on the national map distributed and stored HTML file 
DEF LTC ():
    df=pd.read_excel('text.xlsx')
    province_list = DF [ ' Province ' ] .fillna ( ' from NAN ' ) .ToList () # convert dataframe list of columns, wherein the nan replaced with "from NAN" 
    count_province = pd.value_counts (province_list) # of full frequency list statistical 
    value = count_province.tolist ()
    attr=count_province.index.tolist()
    Map = the Map ( " provincial distribution of micro-channel friends " , width = 1200, height = 600 )
    map.add ( "" , attr, value, maptype = ' china ' , is_visualmap = True, visual_text_color = ' # 000 ' , is_label_show = True) # display province on the map 
    map.show_config ()
    map.render('map.html')

lte ( ' text ' , L) # The friend information into excel file
WTH () # draw word cloud

LTC () # mapping

 

Guess you like

Origin www.cnblogs.com/guyuanlin/p/10964324.html