Files
wechat_crawler/get_current_user.py

32 lines
820 B
Python
Raw Permalink Normal View History

"""
获取当前登录微信的用户名称
"""
from wxauto import WeChat
def get_current_wechat_user():
"""获取当前登录微信的用户名称
Returns:
str: 当前登录微信的用户名称如果获取失败则返回None
"""
try:
# 初始化微信实例
wx = WeChat()
# 获取当前登录用户的昵称
username = wx.nickname
print(f"当前登录的微信用户: {username}")
return username
except Exception as e:
print(f"获取微信用户名称失败: {e}")
return None
if __name__ == "__main__":
user = get_current_wechat_user()
if user:
print(f"\n成功获取用户名称: {user}")
else:
print("\n获取用户名称失败,请确保微信已登录")