python 爬虫 wxpy 获取微信好友的头像和昵称

from wxpy import *
import os


def weixin_file_path():
    avater_dir = os.path.join(os.getcwd(), 'weixin')
    if not os.path.exists(avater_dir):
        os.mkdir(avater_dir)
    return avater_dir


def save_wx_avater(avater_dir):
    bot = Bot(cache_path=False)
    friends = bot.friends(update=True)
    list_name = []
    for friend in friends:
        try:
            friend.get_avatar(os.path.join(avater_dir, f'{str(friend.name)}.jpg'))
            list_name.append(friend.name)
            print("好友昵称:%s" % friend.name)
        except Exception as e:
            print("数据错误",e)
            pass
    with open('微信好友昵称.txt', 'w+', encoding='utf-8')as f:
        for n in list_name:
            f.write("'" + n + "',\n")
        f.close()
        print("程序结束:")
    print(list_name)


if __name__ == '__main__':
    avatar_dir = weixin_file_path()
    save_wx_avater(avatar_dir)

运行结果,和保存的头像,要确保微信能登录网页版哦

猜你喜欢

转载自blog.csdn.net/weixin_43407092/article/details/108012363