點贊、關注、加羣:[313074041]一氣呵成(%E7%82%B9%E5%87%BB%E9%93%BE%E6%8E%A5%E5%8A%A0%E5%85%A5%E7%BE%A4%E8%81%8A%E3%80%90python%E7%BB%BC%E5%90%88%E5%AD%A6%E4%B9%A0%E7%BE%A4%E3%80%91%EF%BC%9Ahttps://jq.qq.com/?_wv=1027&k=5IJRXi6)
準備工作
1、一個可以登錄網頁版微信的微信號
2、一個ide編輯器
3、Python環境基於Python3
安裝相關依賴
能夠登錄微信主要依賴於wxpy這個Python庫
引入Python os 庫
from wxpy import *
import os
主要代碼
# 創建頭像存放文件夾
def create_file_path():
avater_dir = os.path.join(os.getcwd(),'wechat')
if not os.path.exists(avater_dir):
os.mkdir(avater_dir)
return avater_dir
# 獲取所有的好友頭像並保存
def save_wx_avater(avater_dir):
bot = Bot(cache_path=True)
friends = bot.friends(update=True)
num = 0
nameList = []
for friend in friends:
# 保存頭像圖片到指定文件夾
friend.get_avatar(os.path.join(avater_dir,f'{str(friend.name)}.jpg'))
nameList.append(friend.name)
print("好友暱稱:%s"%friend.name)
num += 1
# 遍歷暱稱list並寫入txt
with open('微信好友暱稱.txt', 'w+', encoding='utf-8')as f:
for n in nameList:
f.write("'"+n+"',\n")
f.close()
print("程序結束:")
print(nameList)
執行程序
if __name__ == '__main__':
avatar_dir = create_file_path()
save_wx_avater(avatar_dir)
效果如下