Trích xuất thông tin thành viên từ nhóm Telegram bằng Python

Telegram là một trong những nền tảng tin nhắn được tin dùng nhất trên thế giới. Telegram được sử dụng để quản lý cộng đồng với nhiều mục đích khác nhau.

Các công ty khởi nghiệp tin rằng đây là một công cụ hữu hiệu để thu hút khách hàng cho mục đích quảng bá và dịch vụ. Trong bài hướng dẫn này, sẽ thảo luận về cách sử dụng Telegram API để trích xuất thành viên trong nhóm bằng Telegram API sử dụng Python.

Tạo ứng dụng Telegram:

Đến liên kết my.telegram.org để tạo ứng dụng:

Giao diện trang my.telegram.org

Đăng nhập bằng tài khoản Telegram đã có của bạn >> "Next".

Chọn thẻ API và bổ sung các thông tin yêu cầu để thiết lập ứng dụng.

Điền thông tin để tạo app

Chú ý đến các thông tin ở trường api_idapi_hash; đây là những thông tin liên quan cần sử dụng cho ở các bước tiếp theo (có thể lưu ở nodepad).

Cấu hình app

Cài đặt thư viện telethon

Tìm hiểu thêm về thư viện telethon tại đây.

Trước hết cần cài đặt Python trên máy theo hướng dẫn này, bạn nên cài phiên bản Python 3).

Nhập lệnh:

python pip install telethon

Ghi chú: Hướng dẫn cài đặt trên Windows.

Dưới đây là code chạy cho chương trình. Có thể tham khảo thêm hướng dẫn tạo từng phần theo link cuối bài.

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

Chúc các bạn thành công !!!

Tài liệu tham khảo:
1. https://quantrimang.com/python-la-gi-tai-sao-nen-chon-python-140518
2. https://python.gotrained.com/scraping-telegram-group-members-python-telethon/

Thứ Tư, 05/08/2020 16:55
4,84 👨 5.172
0 Bình luận
Sắp xếp theo
    ❖ Python