import telethon
import sys

api_id =
api_hash = ''


client = TelegramClient('session_name', api_id, api_hash)
client.start()


me = client.get_me()
print(me)

parti = client.get_participants('')
len(parti)
type(parti)



for x in parti:
   print(x)


派对类型:“ telethon.helpers.TotalList”
parti的长度是正确的,因此确实刮擦了正确的用户。
但是当涉及到打印时,它会打印:

User(id=xxxxxxx, is_self=False, contact=False, mutual_contact=False, deleted=False, bot=False, bot_chat_history=False, bot_nochats=False, verified=False, restricted=False, min=False, bot_inline_geo=False, access_hash=3186925291802818105, first_name='vakuumtaucher', last_name=None, username=None, phone=None, photo=UserProfilePhoto(photo_id=2834571071698610090, photo_small=FileLocation(dc_id=2, volume_id=250821725, local_id=227886, secret=-1295815918865284037), photo_big=FileLocation(dc_id=2, volume_id=250821725, local_id=227888, secret=2864116761608555039)), status=UserStatusRecently(), bot_info_version=None, restriction_reason=None, bot_inline_placeholder=None, lang_code=None)
User(id=553795975, is_self=False, contact=False, mutual_contact=False, deleted=False, bot=False, bot_chat_history=False, bot_nochats=False, verified=False, restricted=False, min=False, bot_inline_geo=False, access_hash=3748681057114558961, first_name='René', last_name=None, username='Todtnauer', phone=None, photo=UserProfilePhoto(photo_id=2378535601737672619, photo_small=FileLocation(dc_id=2, volume_id=246934435, local_id=108141, secret=-2834728750137465782), photo_big=FileLocation(dc_id=2, volume_id=246934435, local_id=108143, secret=-7220306442079900232)), status=UserStatusOffline(was_online=datetime.datetime(2018, 11, 24, 10, 38, 31, tzinfo=datetime.timezone.utc)), bot_info_version=None, restriction_reason=None, bot_inline_placeholder=None, lang_code=None)


效果很好,直到弹出带有非BMP字符的用户,因为这样我得到了UnicodeEncodeError。

如何仅将user_id过滤出已打印的用户? (绕过UnicodeEncodeError)?

最佳答案

User是一个对象,要访问对象属性,您必须执行object.property,因此在您的情况下,您必须执行以下操作。

for x in parti:
   print(x.id)

08-06 10:47