前几天统计自己好友性别,看看男女比例,发现竟然还有分类不是男女的,很好奇都是谁,所以空闲下来抓取所有好友看一下。

这边使用了itchat库,网上资料很多。不多说,直接上代码

import itchat
import re
from xlwt import *
# 登录
itchat.login()
# 获取好友列表
friends = itchat.get_friends(update=True)[0:]
file = Workbook(encoding = 'utf-8')
#指定file以utf-8的格式打开
table = file.add_sheet('wx')
table.write(0,0,'姓名')
table.write(0,1,'昵称')
table.write(0,2,'备注')
table.write(0,3,'性别')
table.write(0,4,'签名')
table.write(0,5,'头像')

friends = itchat.get_friends(update=True)[0:]
for key,i in enumerate(friends):
# 获取个性签名
    signature = i["Signature"].strip().replace("span", "").replace("class", "").replace("emoji", "")
# 正则匹配过滤掉emoji表情,例如emoji1f3c3等
    rep = re.compile("1f\d.+")
    signature = rep.sub("", signature)
    NickName = i["NickName"]
    UserName = i["UserName"]
    HeadImgUrl = i["HeadImgUrl"]
    RemarkName = i["RemarkName"]
    Sex = i["Sex"]
    table.write(key+1,0,NickName)
    table.write(key+1,1,UserName)
    table.write(key+1,2,RemarkName)
    table.write(key+1,3,Sex)
    table.write(key+1,4,signature)
    table.write(key+1,5,HeadImgUrl)
file.save('wx.xlsx')

friends = itchat.get_friends(update=True)[0:]获取到微信的好友信息,然后拉取需要的数据,我这边是获取的姓名,昵称,备注,性别,签名,头像。并导入excel表格中

posted on 2019-06-11 10:28  代码书写人生  阅读(782)  评论(0编辑  收藏  举报