使用Python+Telethon能否获取Telegram群组内成员的入群日期?
可以实现该需求。你现有代码拿不到入群日期,是因为直接读取的是用户User对象的属性,Telethon中群成员的入群时间存储在ChannelParticipant对象的date字段中,调整获取成员和写入的逻辑即可:
注意事项
- 该方法仅对超级群、公开频道生效,普通小型私有群组无法批量获取成员入群时间,需通过遍历群内历史入群通知类服务消息的方式匹配对应成员的入群时间。
- 读取到的
date字段默认是UTC标准时间,可根据需要自行转换为对应时区的本地时间。
修改后的完整导出代码
import csv from datetime import datetime # 获取参与者对象的参考代码,替换你原有的all_participants获取逻辑即可 # all_participants = [] # for p in client.iter_participants(target_group): # all_participants.append(p) with open("members.csv","w",encoding='UTF-8') as f: writer = csv.writer(f,delimiter=",",lineterminator="\n") # 表头新增JoinDate列 writer.writerow(['username','user id', 'access hash','name','group', 'group id', 'JoinDate']) for participant in all_participants: user = participant.user # 处理入群日期,转为标准可读字符串格式 join_date = participant.date.strftime("%Y-%m-%d %H:%M:%S") if participant.date else "" username = user.username if user.username else "" first_name = user.first_name if user.first_name else "" last_name = user.last_name if user.last_name else "" name = (first_name + ' ' + last_name).strip() # 写入行新增入群日期字段 writer.writerow([username, user.id, user.access_hash, name, target_group.title, target_group.id, join_date])
内容的提问来源于stack exchange,提问作者user2741620
相关产品推荐
相关产品推荐

