侯体宗的博客
  • 首页
  • Hyperf版
  • beego仿版
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

Python爬虫实现盗取微信好友信息的方法分析

Python  /  管理员 发布于 7年前   280

本文实例讲述了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 reimport jiebaimport numpyimport pandas as pdimport matplotlib.pyplot as pltfrom scipy.misc import imreadfrom wordcloud import WordCloud,ImageColorGeneratorfrom matplotlib.patches import Polygonfrom matplotlib.colors import rgb2hexfrom 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,返回(patches, 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','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({'计数':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程序设计有所帮助。


  • 上一条:
    python 内置函数汇总详解
    下一条:
    python 字符串常用方法汇总详解
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • 在python语言中Flask框架的学习及简单功能示例(0个评论)
    • 在Python语言中实现GUI全屏倒计时代码示例(0个评论)
    • Python + zipfile库实现zip文件解压自动化脚本示例(0个评论)
    • python爬虫BeautifulSoup快速抓取网站图片(1个评论)
    • vscode 配置 python3开发环境的方法(0个评论)
    • 近期文章
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf分页文件功能(0个评论)
    • gmail发邮件报错:534 5.7.9 Application-specific password required...解决方案(0个评论)
    • 欧盟关于强迫劳动的规定的官方举报渠道及官方举报网站(0个评论)
    • 在go语言中使用github.com/signintech/gopdf实现生成pdf文件功能(0个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2018-04
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2022-01
    • 2023-07
    • 2023-10
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客