热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

爬取好友微信信息

importitchatitchat.login()#爬取自己好友相关信息,返回一个json文件friendsitchat.get_friends(updateTrue)[0:
import itchat
itchat.login()
#爬取自己好友相关信息, 返回一个json文件
friends = itchat.get_friends(update=True)[0:]
male = female = other = 0
#friends[0]是自己的信息,所以要从friends[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("男性好友: %.2f%%" % (float(male)/total*100) + "\n" +
"女性好友: %.2f%%" % (float(female) / total * 100) + "\n" +
"不明性别好友: %.2f%%" % (float(other) / total * 100))
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('data.csv', 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)
text = "".join(siglist)

  


推荐阅读
author-avatar
曾理Aiheqihao_227
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有