Python學習筆記 獲取好友資訊

2021-08-20 19:41:12 字數 2998 閱讀 1399

一、**實現:

# -*- coding:utf-8 -*-

# python2

import itchat

itchat.login()

# 爬取自己好友相關資訊,返回乙個json檔案

friends = itchat.get_friends(update=true)[0:]

#print friends

male = female = other = 0

for i in friends[1:]: # friengs[0]是自己的資訊

*** = i['***']

if *** == 1:

male += 1

elif *** == 2:

female += 1

else:

other += 1

total = len(friends[1:])

print

"男性好友: %.2f%%" % (float(male)/total*100) + '\n' + "女性好友: %.2f%%" % (float(female)/total*100) + '\n' +\

"不明性別好友: %.2f%%" % (float(other)/total*100)

# 首先定義乙個函式爬去各個變數

defget_var

(var):

variable =

for i in friends:

value = i[var]

if type(value) != int:

value = value.encode('utf-8')

return variable

nickname = get_var('nickname')

#print nickname

*** = get_var('***')

province = get_var('province')

city = get_var('city')

remarkname = get_var('remarkname') # 備註名

signature = get_var('signature')

from pandas import dataframe

data =

frame = dataframe(data)

frame.to_csv('data.csv')

# 首先將簽名表示出來,並去除各種表情什麼的,再用正規表示式去除《>=等符號

import re

siglist =

for i in friends:

signature = i['signature'].encode('utf-8').strip().replace('span','').replace('class','').replace('emoji',"")

print signature

rep = re.compile("1f\d+\w*|[<>/=]") # 正規表示式???沒懂。。。

signature = rep.sub('',signature)

print siglist

text = "".join(siglist)

print text.replace(" ","").replace("\n","")

# 結巴分詞包,對字串進行分詞

# 它是python最好的中文分詞元件;

import jieba

wordlist = jieba.cut(text,cut_all=true) # 全模式;false為精確模式;無為預設模式;cut_search_for()為搜尋引擎模式

word_space_split = " ".join(wordlist)

print word_space_split

# 畫圖

import matplotlib.pyplot as plt

from wordcloud import wordcloud,imagecolorgenerator

import numpy as np

import pil.image as image

coloring = np.array(image.open(r"c:\users\zhyu\pycharmprojects\wechat\1.png"))

my_wordcloud = wordcloud(background_color='white',max_words=2000,mask=coloring,max_font_size=60,random_state=42,\

scale=2,font_path=r"c:\users\zhyu\downloads\notosans-hinted\notosans-black.ttf").generate(word_space_split)

image_colors = imagecolorgenerator(coloring)

plt.imshow(my_wordcloud.recolor(color_func=image_colors))

plt.imshow(my_wordcloud)

plt.axis("off")

plt.show()

itchat.logout()

執行結果:

二、遇到的問題

1、編碼問題:由於用search_friends()得到的是所有好友資訊的json資料,所以得到的暱稱、簽名等都是u』\u789』這種unicode形式,需要將其轉為漢字表示時,用encode(「utf-8」)即可。

2、安裝pil遇到的問題,直接用pip安裝後,執行報錯:

importerror: the _imagingft c module

isnot installed

3、最後的雲圖出現了亂碼……此問題還未解決……下午再看

Python 實現獲取微信好友資訊

一 使用 wxpy 模組庫獲取好友男比例資訊和城市分布。coding utf 8 匯入模組 from wxpy import bot q 1 極簡模式 robot bot 2 終端模式 robot bot console qr true 3 快取模式 可保持登入狀態 robot bot cache ...

python微信好友 Python簡單分析微信好友

開發工具 python版本 3.6.4 itchat模組 pandas模組 pyecharts模組。環境搭建 安裝python並新增到環境變數,pip安裝需要的相關模組即可。相關檔案 額外說明 pyecharts模組安裝時請依次執行以下命令 pip install echarts countries...

python 給微信好友發訊息

傳送搞定了,接收沒搞定,有時間再搞 pip install wxpy from future import unicode literals from threading import timer from wxpy import import requests 機械人 bot bot defget...