从 Telegram 聊天中抓取在线和最近最后一次看到的用户

问题描述 投票:0回答:3

我想使用 Telethon API 抓取 Telegram 群组。但这段代码会抓取组中的所有用户。但我只想抓取最近在线和最后一次看到的用户。怎么办?

from telethon.sync import TelegramClient
from telethon.tl.functions.messages import GetDialogsRequest
from telethon.tl.types import InputPeerEmpty
import csv

api_id = 123456
api_hash = 'YOUR_API_HASH'
phone = '+111111111111'
client = TelegramClient(phone, api_id, api_hash)

client.connect()
if not client.is_user_authorized():
    client.send_code_request(phone)
    client.sign_in(phone, input('Enter the code: '))


chats = []
last_date = None
chunk_size = 200
groups=[]
 
result = client(GetDialogsRequest(
             offset_date=last_date,
             offset_id=0,
             offset_peer=InputPeerEmpty(),
             limit=chunk_size,
             hash = 0
         ))
chats.extend(result.chats)

for chat in chats:
    try:
        if chat.megagroup== True:
            groups.append(chat)
    except:
        continue

print('Choose a group to scrape members from:')
i=0
for g in groups:
    print(str(i) + '- ' + g.title)
    i+=1

g_index = input("Enter a Number: ")
target_group=groups[int(g_index)]

print('Fetching Members...')
all_participants = []
all_participants = client.get_participants(target_group, aggressive=True)

print('Saving In file...')
with open("members.csv","w",encoding='UTF-8') as f:
    writer = csv.writer(f,delimiter=",",lineterminator="\n")
    writer.writerow(['username','user id', 'access hash','name','group', 'group id'])
    for user in all_participants:
        if user.username:
            username= user.username
        else:
            username= ""
        if user.first_name:
            first_name= user.first_name
        else:
            first_name= ""
        if user.last_name:
            last_name= user.last_name
        else:
            last_name= ""
        name= (first_name + ' ' + last_name).strip()
        writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id])      
print('Members scraped successfully.')
python telegram telegram-bot python-telegram-bot telethon
3个回答
0
投票

希望这有帮助:

...
for user in all_participants:
    if not user.user.status in ['online','recently']: continue 
    if user.username:
        username= user.username
...

0
投票

我尝试了这个代码和另一个具有

num.date
条件的代码,但向我展示了一个成员,甚至什么也不显示,但我认为最好改变一件简单的事情,最后使用Excel过滤器来得到答案。最近一次出现是在上线前 1 秒到 2 或 3 天...

with open("members.csv","w",encoding='UTF-8') as f:
    writer = csv.writer(f,delimiter=",",lineterminator="\n")
    writer.writerow(['username','user id', 'access hash','name','group', 'group id','status'])
    for user in all_participants:
        if user.username:
            username= user.username
        else:
            username= ""
        if user.first_name:
            first_name= user.first_name
        else:
            first_name= ""
        if user.last_name:
            last_name= user.last_name
        else:
            last_name= ""
        name= (first_name + ' ' + last_name).strip()
        writer.writerow([username,user.id,user.access_hash,name,target_group.title, target_group.id,user.status])      
print('Members scraped successfully.')

你可以在最后添加

user.status
就可以了。


0
投票

Появление такой записи 最后一次出现是在 может быть расшифровано по-разному, в зависимости от конкретной ситуации。 Данная статья предлагает разбор типичных примеров размещения подобного сообщения:

https://101info.ru/last-seen-recently-perevod-na-russkii/

С помощью инструкций этого материала можно без труда настроить эту функцию в соответствии с вашими индивидуа льными предпочтениями.

© www.soinside.com 2019 - 2024. All rights reserved.