3
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)

parti 的类型:'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)?

4

1 回答 1

2

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

for x in parti:
   print(x.id)
于 2019-02-20T21:34:23.743 回答