python - 如何从 Telegram 组中抓取 Telegram 用户?
问题描述
所以我尝试了很长时间,我找到了一些预制的程序,但它们的成本是 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.')
我输入了我的信息,并启动了程序,但我不断收到此错误。
回溯(最后一次调用):文件“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 行,调用 结果 = await future telethon.errors.rpcerrorlist.AuthKeyUnregisteredError:密钥未在系统中注册(由 GetDialogsRequest 引起)
我到处搜索修复或教程,但没有找到任何东西。我唯一的另一个选择就是来这里。
请帮忙。
问候,丹尼尔
解决方案
诚然,文档并不太清楚该错误的含义,但从它的外观来看,您可能会受到非托管资源的影响。文档本身在这里建议:
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 做出贡献的开发人员之一已经编写了一个免费和开源的爬虫?
推荐阅读
- r - 如何通过 optim() 返回接近估计来启用优化?
- visual-studio - VS 单元测试项目缺少 NuGet 包
- amazon-web-services - 在 terraform 中创建多个负载均衡器、目标组和侦听器
- javascript - 在页面上向下滚动时需要缩小分区内的徽标图像
- javascript - 如何使用选择选项作为该函数的参数来调用更改选择选项的函数
- javascript - 模块“SharedModule”导入的意外值“未定义”
- css - 带有 Flexbox 应用程序根的 Angular 不是 100% 宽
- c - 将UTC时间戳转换为C中的纪元时间
- freeradius - 我在哪里可以修改 Freeradius 3x 中的用户名验证?
- firebird - 连接到 Firebird 数据库时出现 BLR 错误