日韩久久久精品,亚洲精品久久久久久久久久久,亚洲欧美一区二区三区国产精品 ,一区二区福利

Python使用微信itchat接口實現查看自己微信的信息功能詳解

系統 1818 0

本文實例講述了Python使用微信itchat接口實現查看自己微信的信息功能。分享給大家供大家參考,具體如下:

itchat是python的一個api,可以訪問自己的微信信息,功能還蠻好玩的,可以扒取朋友信息,自動回復短信等等。

package:

itchat1.3.10 + python3.5 + wordcloud1.4.1

登錄登出:

            
itchat.login()
#hotReload設置為True,可以保持一段時間登錄
itchat.autologin(hotReload=True)
itchat.logout()


          

獲取朋友數據:

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


          

搜索某個朋友:

            
itchat.search_friends(name='name')
itchat.search_friends(wechatAccount='wechatid')


          

公眾號和群聊的獲取方式也是類似的:

            
itchat.get_mps(update=True)[0:]
itchat.search_mps()
itchat.get_chatrooms(update=True)[0:]
itchat.search_chatroom()


          

發信息:

            
itchat.send(msg='Received Your Message',toUserName=userName])
#username其實是一個id,nickname是微信名字,remarkname是備注名


          

自動回復信息:

            
@itchat.msg_register(itchat.content.TEXT)
def simple_reply(recv_msg):
  msg = recv_msg['Text']
  if msg == 'name':
    itchat.send(msg=u'Received name from',toUserName=recv_msg['FromUserName'])
  elif msg == 'age':
    itchat.send(msg=u'Received age from',toUserName=recv_msg['FromUserName'])
itchat.run()
#register也接受其他參數,比如說isGroupChat=True用來只自動回復群聊信息


          

register還可以注冊其他參數:

MAP 地理位置的分享
CARD 名片信息
SHARING 鏈接分享
PICTURE 表情或照片
RECORDING 語音
ATTACHMENT 附件
VIDEO 視頻
FRIENDS 加好友申請,也就是說發起的一個好友申請其實是一條特殊的信息
SYSTEM 系統消息,比如系統推送消息或者是某個群成員發生變動等等
NOTE 通知文本,比如撤回了消息等等

例子:拉取朋友數據,用wordcloud可視化朋友signature

先讀取數據

            
import itchat
itchat.login()
friends = itchat.get_friends(update=True)[0:]


          

簡單分析下性別比例

            
male = female = other = 0
#friends[0] is personal information, friends start from 1
for i in friends[1:]:
  sex = i["Sex"]
  if sex == 1:
    male += 1
  elif sex == 2:
    female += 1
  else:
    other +=1
total = len(friends[1:])
print("male: %.2f%%" % (float(male)/total*100) + "\n" +
"female: %.2f%%" % (float(female) / total * 100) + "\n" +
"unknown: %.2f%%" % (float(other) / total * 100))


          

獲得各個參數,存入本地

            
filename = '' #需要修改這里
#爬取各個變量
def get_var(var):
  variable = []
  for i in friends:
    value = i[var]
    variable.append(value)
  return variable
#把數據存到csv文件中,保存到桌面
NickName = get_var("NickName")
Sex = get_var('Sex')
Province = get_var('Province')
City = get_var('City')
Signature = get_var('Signature')
from pandas import DataFrame
data = {'NickName': NickName, 'Sex': Sex, 'Province': Province,
    'City': City, 'Signature': Signature}
frame = DataFrame(data)
frame.to_csv(filename, index=True)


          

去除特殊字符和轉義字符等

            
import re
siglist = []
for i in friends:
  signature = i["Signature"].strip().replace("span","").replace("class","").replace("emoji","")
  rep = re.compile("1f\d+\w*|[<>/=]")
  signature = rep.sub("", signature)
  siglist.append(signature)


          

查看signature列表

            
#去掉末尾的空格以及空的簽名
while '' in siglist:
  siglist.remove('')
for i in range(len(siglist)):
  siglist[i].strip()
  print(i,siglist[i])
#wordcloud讀取數據要求為string,以空格隔開
text = "".join(siglist)


          

可視化簽名

            
import matplotlib.pyplot as plt
from wordcloud import WordCloud, ImageColorGenerator
import numpy as np
import PIL.Image as Image
picture_path = '' #這里需要修改
coloring = np.array(Image.open(picture_path))
my_wordcloud = WordCloud(background_color="white", max_words=2000, font_path="2.ttf",
             mask = coloring, max_font_size=60, random_state=42, scale=2).generate(text)
image_colors = ImageColorGenerator(coloring)
plt.imshow(my_wordcloud.recolor(color_func=image_colors))
plt.imshow(my_wordcloud)
plt.axis("off")
plt.show()


          

保存:

            
save_path = '' #這里需要修改
my_wordcloud.to_file(save_path)


          

這里是以自己選的picture為形狀,生成詞云,以下是我的生成結果:

? Python使用微信itchat接口實現查看自己微信的信息功能詳解_第1張圖片

更多關于Python相關內容感興趣的讀者可查看本站專題:《Python數學運算技巧總結》、《Python數據結構與算法教程》、《Python函數使用技巧總結》、《Python字符串操作技巧匯總》、《Python入門與進階經典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對大家Python程序設計有所幫助。


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 开远市| 海门市| 金秀| 铁岭市| 安远县| 张家口市| 富平县| 三穗县| 杭锦后旗| 兴城市| 清水河县| 突泉县| 德庆县| 五常市| 花莲市| 新和县| 博白县| 望奎县| 息烽县| 汨罗市| 克拉玛依市| 枞阳县| 广河县| 汪清县| 集安市| 马龙县| 友谊县| 翼城县| 易门县| 纳雍县| 茌平县| 德格县| 金川县| 同德县| 上蔡县| 彭泽县| 新民市| 尼勒克县| 札达县| 马山县| 湖口县|