用Python爬取个人微信朋友信息

利用Python的itchat包爬取个人微信号的朋友信息,并将信息保存在本地文本中

思路要点:
1.利用itchat.login(),实现微信号的扫码登录
2.通过itchat.get_friends()函数获取朋友信息

代码:
本文代码只获取了几个常用的信息,更多信息可从itchat.get_friends()中取

#获取个人微信号中朋友信息
#导入itchat包
import itchat

#获取个人微信号好友信息
if __name__=="__main__":
    #登录个人微信,扫码登录
    itchat.login()
    #爬取自己好友相关信息
    friends=itchat.get_friends(update=False)[0:]
    #设置需要爬取的信息字段
    result=[('RemarkName','备注'),('NickName','微信昵称'),('Sex','性别'),('City','城市'),('Province','省份'),('ContactFlag','联系标识'),('UserName','用户名'),('SnsFlag','渠道标识'),('Signature','个性签名')]
    for user in friends:
        with open('myFriends.txt','a',encoding='utf8') as fh:
            fh.write("-----------------------\n")
        for r in result:
            with open('myFriends.txt','a',encoding='utf8') as fh:
                fh.write(r[1]+":"+str(user.get(r[0]))+"\n")
    print("完成")

猜你喜欢

转载自blog.csdn.net/d1240673769/article/details/75201195
今日推荐