1. 程式人生 > 程式設計 >Python爬蟲實現“盜取”微信好友資訊的方法分析

Python爬蟲實現“盜取”微信好友資訊的方法分析

本文例項講述了Python爬蟲實現“盜取”微信好友資訊的方法。分享給大家供大家參考,具體如下:

剛起床,閒來無聊,找點事做,看了朋友圈一篇爬取微信好友資訊的文章,突發奇想,偷偷看看女朋友微信有些啥。。。。於是就下手了。。。。[陰險]

1、準備工作:

執行平臺:Windows

Python版本:Python3.6

IDE:Sublime Text

Python庫:wxpy,

2、開發流程:(電腦沒電了,要撐不住了啦~之後具體分析)

3、直接上程式碼:

# 微信好友資訊爬取+資料視覺化
# encoding=utf-8
__author__ = 'Jonny'
__location__ = '濟南'
__date__ = '2018-06-02'
from wxpy import *
import re
import jieba
import numpy
import pandas as pd
import matplotlib.pyplot as plt
from scipy.misc import imread
from wordcloud import WordCloud,ImageColorGenerator
from matplotlib.patches import Polygon
from matplotlib.colors import rgb2hex
from mpl_toolkits.basemap import B
# 微信登入
def wx_login():
  try:
    #初始化機器人,掃碼登入
    bot = Bot()
    #獲取好友列表
    frinds = bot.friends()
    #wxpy.api.chats.chats.Chats物件是多個聊天物件的合集,
    # 可用於搜尋或統計,可以搜尋和統計的資訊包括sex(性別)、province(省份)、city(城市)和signature(個性簽名)等
    print(type(frinds))
    #輸出好友列表
    for i in frinds:
      print(i)
  except Exception as e:
    print(e.args)
    wx_login()
  return frinds
# 資料視覺化
#統計男女性別資訊
def wx_friend_sex_infor(friends):
  sex_dict = {'male':0,'female':0,'other':0}
  for friend in friends:
    if friend.sex == 1:
      sex_dict['male'] += 1
    elif friend.sex == 2:
      sex_dict['female'] += 1
    else:
      print(friend,'性別未標記!')
      sex_dict['other'] += 1
  print(sex_dict)
  wx_show_sex_infor(sex_dict)
# pie(x,explode=None,labels=None,#   colors=('b','g','r','c','m','y','k','w'),#   autopct=None,pctdistance=0.6,shadow=False,#   labeldistance=1.1,startangle=None,radius=None,#   counterclock=True,wedgeprops=None,textprops=None,#   center = (0,0),frame = False )
# 引數說明
# x    (每一塊)的比例,如果sum(x) > 1會使用sum(x)歸一化
# labels (每一塊)餅圖外側顯示的說明文字
# explode (每一塊)離開中心距離
# startangle 起始繪製角度,預設圖是從x軸正方向逆時針畫起,如設定=90則從y軸正方向畫起
# shadow 是否陰影
# labeldistance label繪製位置,相對於半徑的比例,如<1則繪製在餅圖內側
# autopct 控制餅圖內百分比設定,可以使用format字串或者format function
#     '%1.1f'指小數點前後位數(沒有用空格補齊)
# pctdistance 類似於labeldistance,指定autopct的位置刻度
# radius 控制餅圖半徑
# 返回值:
# 如果沒有設定autopct,返回(patches,texts)
# 如果設定autopct,texts,autotexts)
def wx_show_sex_infor(data):
  labers = ['男性','女性','未標記']
  data = [data['male'],data['female'],data['other']]
  plt.pie(data=data,labels=labers,autopct='%.2f',shadow=True)
  plt.show()
  plt.savefig('sex.jpg')
  plt.close()
def wx_friend_location_infor(friends):
  loction_dict = {'北京': 0,'上海': 0,'天津': 0,'重慶': 0,'河北': 0,'山西': 0,'吉林': 0,'遼寧': 0,'黑龍江': 0,'陝西': 0,'甘肅': 0,'青海': 0,'山東': 0,'福建': 0,'浙江': 0,'臺灣': 0,'河南': 0,'湖北': 0,'湖南': 0,'江西': 0,'江蘇': 0,'安徽': 0,'廣東': 0,'海南': 0,'四川': 0,'貴州': 0,'雲南': 0,'內蒙古': 0,'新疆': 0,'寧夏': 0,'廣西': 0,'西藏': 0,'香港': 0,'澳門': 0}
  for friend in friends:
    if friend.province in loction_dict.keys():
      loction_dict[friend.province] += 1
  #轉成JSON格式:
  loction_list = []
  for key,value in loction_dict.items():
    loction_list.append({'name':key,'sum':value})
  print(loction_list)
def wx_show_location_infor():
  pass
#顯示好友個籤資訊
def wx_show_signature(friends):
  #統計好友簽名
  for friend in friends:
    #對資料進行清洗,排除標點資訊的干擾
    pattern = re.compile(r'[一-龥]+')
    filterdata = re.findall(pattern,friend.signature)
    with open('signature.txt','a',encoding='utf-8',newline='') as f:
      f.write(str(friend)+''.join(filterdata)+'\n')
  f.close()
  # 讀取檔案資料
  with open('signature.txt',newline='') as f:
    content = f.read()
  f.close()
  segment = jieba.lcut(content)
  words_df = pd.DataFrame({'segment':segment})
  #讀取stopwords
  stopwords = pd.read_csv('stopwords.txt',index_col=False,quoting=3,sep=' ',names=['stopword'],encoding='gb18030')
  words_df = words_df[~words_df.segment.isin(stopwords.stopword)]
  print(words_df)
  words_stat = words_df.groupby(by=['segment'])['segment'].agg({'計數':numpy.size})
  words_stat = words_stat.reset_index().sort_values(by=['計數'],ascending=False)
  #設定詞雲屬性
  color_mask = imread('background.jpg')
  wordcloud = WordCloud(font_path='simhei.ttf',#設定字型可以顯示中文
             background_color= 'white',#背景顏色是白色
             max_words=1000,#設定詞雲顯示的最大詞數
             mask=color_mask,#設定背景圖片
             max_font_size=400,#設定詞雲中字型的最大值
             random_state=42,width=500,height=430,margin=2,#設定圖片預設大小
  )
  # 生成詞雲,可以用generate輸入全部文字,也可以我們計算好詞頻後使用generate_from_frequencies函式
  word_frequence = {x[0]: x[1] for x in words_stat.head(100).values}
  print(word_frequence)
  word_frequence_dict = {}
  for key in word_frequence:
    word_frequence_dict[key] = word_frequence[key]
  wordcloud.generate_from_frequencies(word_frequence_dict)
  # 從背景圖片生成顏色值
  image_colors = ImageColorGenerator(color_mask)
  # 重新上色
  wordcloud.recolor(color_func=image_colors)
  # 儲存圖片
  wordcloud.to_file('output.png')
  plt.imshow(wordcloud)
  plt.axis("off")
  plt.show()
  plt.close()
if __name__ == '__main__':
  friends = wx_login()
  print('~~~~~~~~~~~~~~~~~~~~1~~~~~~~~~~~~~~~~~~~~~~~~~~~~')
  wx_friend_sex_infor(friends)
  print('~~~~~~~~~~~~~~~~~~~~~2~~~~~~~~~~~~~~~~~~~~~~~~~~~')
  wx_friend_location_infor(friends)
  print('~~~~~~~~~~~~~~~~~~~~~~3~~~~~~~~~~~~~~~~~~~~~~~~~~')
  wx_show_signature(friends)
  print('~~~~~~~~~~~~~~~~~~~~~~~4~~~~~~~~~~~~~~~~~~~~~~~~~')

更多關於Python相關內容可檢視本站專題:《Python Socket程式設計技巧總結》、《Python正則表示式用法總結》、《Python資料結構與演算法教程》、《Python函式使用技巧總結》、《Python字串操作技巧彙總》、《Python入門與進階經典教程》及《Python檔案與目錄操作技巧彙總》

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