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

Python爬蟲實(shí)現(xiàn)“盜取”微信好友信息的方法分析

系統(tǒng) 1867 0

本文實(shí)例講述了Python爬蟲實(shí)現(xiàn)“盜取”微信好友信息的方法。分享給大家供大家參考,具體如下:

剛起床,閑來無聊,找點(diǎn)事做,看了朋友圈一篇爬取微信好友信息的文章,突發(fā)奇想,偷偷看看女朋友微信有些啥。。。。于是就下手了。。。。[陰險(xiǎn)]

1、準(zhǔn)備工作:

運(yùn)行平臺(tái):Windows

Python版本:Python3.6

IDE:Sublime Text

Python庫(kù):wxpy,

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

3、直接上代碼:

            
# 微信好友信息爬取+數(shù)據(jù)可視化
# encoding=utf-8
__author__ = 'Jonny'
__location__ = '濟(jì)南'
__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:
    #初始化機(jī)器人,掃碼登錄
    bot = Bot()
    #獲取好友列表
    frinds = bot.friends()
    #wxpy.api.chats.chats.Chats對(duì)象是多個(gè)聊天對(duì)象的合集,
    # 可用于搜索或統(tǒng)計(jì),可以搜索和統(tǒng)計(jì)的信息包括sex(性別)、province(省份)、city(城市)和signature(個(gè)性簽名)等
    print(type(frinds))
    #輸出好友列表
    for i in frinds:
      print(i)
  except Exception as e:
    print(e.args)
    wx_login()
  return frinds
# 數(shù)據(jù)可視化
#統(tǒng)計(jì)男女性別信息
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,'性別未標(biāo)記!')
      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 )
# 參數(shù)說明
# x    (每一塊)的比例,如果sum(x) > 1會(huì)使用sum(x)歸一化
# labels (每一塊)餅圖外側(cè)顯示的說明文字
# explode (每一塊)離開中心距離
# startangle 起始繪制角度,默認(rèn)圖是從x軸正方向逆時(shí)針畫起,如設(shè)定=90則從y軸正方向畫起
# shadow 是否陰影
# labeldistance label繪制位置,相對(duì)于半徑的比例, 如<1則繪制在餅圖內(nèi)側(cè)
# autopct 控制餅圖內(nèi)百分比設(shè)置,可以使用format字符串或者format function
#     '%1.1f'指小數(shù)點(diǎn)前后位數(shù)(沒有用空格補(bǔ)齊)
# pctdistance 類似于labeldistance,指定autopct的位置刻度
# radius 控制餅圖半徑
# 返回值:
# 如果沒有設(shè)置autopct,返回(patches, texts)
# 如果設(shè)置autopct,返回(patches, texts, autotexts)
def wx_show_sex_infor(data):
  labers = ['男性','女性','未標(biāo)記']
  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, '臺(tái)灣': 0, '河南': 0, '湖北': 0, '湖南': 0,
           '江西': 0, '江蘇': 0, '安徽': 0, '廣東': 0, '海南': 0,
           '四川': 0, '貴州': 0, '云南': 0,
           '內(nèi)蒙古': 0, '新疆': 0, '寧夏': 0, '廣西': 0, '西藏': 0,
           '香港': 0, '澳門': 0}
  for friend in friends:
    if friend.province in loction_dict.keys():
      loction_dict[friend.province] += 1
  #轉(zhuǎn)成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
#顯示好友個(gè)簽信息
def wx_show_signature(friends):
  #統(tǒng)計(jì)好友簽名
  for friend in friends:
    #對(duì)數(shù)據(jù)進(jìn)行清洗,排除標(biāo)點(diǎn)信息的干擾
    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()
  # 讀取文件數(shù)據(jù)
  with open('signature.txt','r',encoding='utf-8',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({'計(jì)數(shù)':numpy.size})
  words_stat = words_stat.reset_index().sort_values(by=['計(jì)數(shù)'],ascending=False)
  #設(shè)置詞云屬性
  color_mask = imread('background.jpg')
  wordcloud = WordCloud(font_path='simhei.ttf',    #設(shè)置字體可以顯示中文
             background_color= 'white',  #背景顏色是白色
             max_words=1000,        #設(shè)置詞云顯示的最大詞數(shù)
             mask=color_mask,       #設(shè)置背景圖片
             max_font_size=400,      #設(shè)置詞云中字體的最大值
             random_state=42,
             width=500,height=430,margin=2,#設(shè)置圖片默認(rèn)大小
  )
  # 生成詞云, 可以用generate輸入全部文本,也可以我們計(jì)算好詞頻后使用generate_from_frequencies函數(shù)
  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~~~~~~~~~~~~~~~~~~~~~~~~~')


          

更多關(guān)于Python相關(guān)內(nèi)容可查看本站專題:《Python Socket編程技巧總結(jié)》、《Python正則表達(dá)式用法總結(jié)》、《Python數(shù)據(jù)結(jié)構(gòu)與算法教程》、《Python函數(shù)使用技巧總結(jié)》、《Python字符串操作技巧匯總》、《Python入門與進(jìn)階經(jīng)典教程》及《Python文件與目錄操作技巧匯總》

希望本文所述對(duì)大家Python程序設(shè)計(jì)有所幫助。


更多文章、技術(shù)交流、商務(wù)合作、聯(lián)系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號(hào)聯(lián)系: 360901061

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

【本文對(duì)您有幫助就好】

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

發(fā)表我的評(píng)論
最新評(píng)論 總共0條評(píng)論
主站蜘蛛池模板: 呼和浩特市| 阆中市| 石台县| 汤原县| 宁海县| 双辽市| 焉耆| 鹤壁市| 慈利县| 莆田市| 武山县| 肃宁县| 阜康市| 麻城市| 紫金县| 永登县| 阿拉善盟| 宿州市| 大同市| 漳浦县| 孝昌县| 荣成市| 西安市| 高清| 怀集县| 墨脱县| 延庆县| 屯留县| 柏乡县| 武鸣县| 汕尾市| 工布江达县| 宜州市| 荣成市| 朔州市| 安塞县| 桐柏县| 涿鹿县| 城市| 开原市| 漾濞|