【问题标题】:How can I scrape Telegram users from a Telegram Group?如何从 Telegram 组中抓取 Telegram 用户?
【发布时间】:2019-09-06 14:03:44
【问题描述】:

所以我尝试了很长时间,我找到了一些预制程序,但它们的成本是 100 美元。我尝试了多个应用程序和程序,例如 Telegram Auto 和 Telegram Kit,但它们的成本很高,而且我现在没有这么多钱。

我正在尝试用 Python 和 Telethon 来做(没有太多经验)

我已经在电报开发者工具上做了一个app,拿到了API Number和Hash,在网上找到了如下代码

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

api_id = My API ID
api_hash = 'MY API HASH'
phone = 'MY PHONE'
client = TelegramClient(phone, api_id, api_hash)

client.connect()

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.')

我输入了我的信息,并启动了程序,但我不断收到此错误。

Traceback(最近一次调用最后一次): 文件“c:\Users\User\Desktop\export.py”,第 23 行,在 哈希 = 0 文件“C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telethon\sync.py”,第 39 行,同步 返回 loop.run_until_complete(coro) 文件“C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\asyncio\base_events.py”,第 579 行,在 run_until_complete 返回future.result() 调用中的文件“C:\Users\User\AppData\Local\Programs\Python\Python37-32\lib\site-packages\telethon\client\users.py”,第 64 行 结果 = 等待未来 telethon.errors.rpcerrorlist.AuthKeyUnregisteredError:密钥未在系统中注册(由GetDialogsRequest引起)

我到处搜索修复或教程,但没有找到任何东西。我唯一的另一个选择就是来这里。

请帮忙。

问候,丹尼尔

【问题讨论】:

  • 这被认为是垃圾邮件,并且违反了电报的 ToS。如果您这样做,您的帐户将被禁止。

标签: python bots telegram telethon


【解决方案1】:

诚然,文档 aren't very clear 关于该错误的含义,但从它的外观来看,您可能会受到非托管资源的影响。文档本身建议here:

TelegramClient 聚合了几个 mixin 类,以一个漂亮的 Pythonic 接口提供所有常用功能。每个 mixin 都有自己的方法,大家都可以使用。

简而言之,要创建客户端,您必须运行:

   from telethon import TelegramClient

   client = TelegramClient(name, api_id, api_hash)

   async def main():
       # Now you can use all client methods listed below, like for example...
       await client.send_message('me', 'Hello to myself!')

   with client:
       client.loop.run_until_complete(main())

您不需要导入这些 AuthMethods、MessageMethods 等。它们一起是 TelegramClient,您可以访问它们的所有方法。

有关简短摘要,请参阅客户参考。

考虑使用python的with语句来帮助管理client

顺便说一句,您是否知道其中一位为 Telethon 做出贡献的开发人员已经写了一个 free and open source scraper?

【讨论】:

  • 错误直接来自电报服务器,没有记录。
猜你喜欢
  • 2022-07-09
  • 2017-11-25
  • 2019-02-14
  • 2022-07-28
  • 2016-04-04
  • 2016-02-23
  • 1970-01-01
  • 2018-11-04
  • 1970-01-01
相关资源
最近更新 更多