Python Telethon:抓取并存储 Telegram 消息
Python Telethon: Scrape and store Telegram messages
我以前用 R 编写代码,但最近切换回 Python。对于一个关于仇恨言论的研究项目,我喜欢在数据框中使用 telethon 显示和存储来自 Telegram 频道的消息。我需要存储数据,因为我想以计算方式可视化和分析它。我习惯了 pandas 数据框,但也对其他替代方案感到满意。我正在使用 Python 3.7 和 Spyder IDE.
使用 我可以获取并显示我所属频道内的消息。
from telethon.sync import TelegramClient
name = 'anon'
api_id = 'myAPI_ID'
api_hash = "myAPI_hash"
chat = 'chat_link'
async with TelegramClient(name, api_id, api_hash) as client:
async for message in client.iter_messages(chat):
print(message.sender_id, ':', message.text)
我以为我可以创建一个新变量来存储显示的数据,但我发现它并不那么微不足道,部分原因还在于协程。下面的代码行创建了一个新变量,但我不知道如何将数据存储在 (pandas) 数据框中。我什至不确定它是否存储了正确类型的数据。
participants = message.sender_id
While the Telethon documentation is explains really nicely how to display messages, there is no example how to store messages. I am aware that the same question has been asked before, but without an answer. I have also looked at this tutorial 解释了如何挖掘和存储消息,但我无法使其工作。第一个问题出现在第 5 行代码 [Telegram] 中。即使我将不同的代码行修补在一起,GetParticipantsRequest 命令也不适用于我不是管理员的频道。
如何继续将显示的消息和用户 ID 存储在数据框中?
感谢您的帮助。
据我所知,你的问题更多是关于 Python 和 Pandas 而不是 Telegram 和 Telethon。
from telethon.sync import TelegramClient
name = 'anon'
api_id = 'myAPI_ID'
api_hash = "myAPI_hash"
chat = 'chat_link'
async with TelegramClient(name, api_id, api_hash) as client:
async for message in client.iter_messages(chat):
print(message.sender_id, ':', message.text)
使用此代码,您将迭代 Telegram 聊天的消息,然后打印发送的 ID 和消息文本。
要将它们存储在变量中,您只需更改
print(message.sender_id, ':', message.text)
到
sender, text = message.sender_id, message.text
您可以将数据附加到列表,然后将其保存到 pandas 数据框。
将这一切结合在一起
import pandas as pd
from telethon.sync import TelegramClient
name = 'anon'
api_id = 'myAPI_ID'
api_hash = 'myAPI_hash'
chat = 'chat_link'
data = [] # stores all our data in the format SENDER_ID, MSG
async with TelegramClient(name, api_id, api_hash) as client:
async for message in client.iter_messages(chat):
data.append([message.sender_id, message.text])
df = pd.DataFrame(data, columns=['SENDER', 'MESSAGE']) # creates a new dataframe
df.to_csv('filename.csv', encoding='utf-8') # save to a CSV file
注意:在聊天中重复消息时注意 API 限制。
我以前用 R 编写代码,但最近切换回 Python。对于一个关于仇恨言论的研究项目,我喜欢在数据框中使用 telethon 显示和存储来自 Telegram 频道的消息。我需要存储数据,因为我想以计算方式可视化和分析它。我习惯了 pandas 数据框,但也对其他替代方案感到满意。我正在使用 Python 3.7 和 Spyder IDE.
使用
from telethon.sync import TelegramClient
name = 'anon'
api_id = 'myAPI_ID'
api_hash = "myAPI_hash"
chat = 'chat_link'
async with TelegramClient(name, api_id, api_hash) as client:
async for message in client.iter_messages(chat):
print(message.sender_id, ':', message.text)
我以为我可以创建一个新变量来存储显示的数据,但我发现它并不那么微不足道,部分原因还在于协程。下面的代码行创建了一个新变量,但我不知道如何将数据存储在 (pandas) 数据框中。我什至不确定它是否存储了正确类型的数据。
participants = message.sender_id
While the Telethon documentation is explains really nicely how to display messages, there is no example how to store messages. I am aware that the same question has been asked before, but without an answer. I have also looked at this tutorial 解释了如何挖掘和存储消息,但我无法使其工作。第一个问题出现在第 5 行代码 [Telegram] 中。即使我将不同的代码行修补在一起,GetParticipantsRequest 命令也不适用于我不是管理员的频道。
如何继续将显示的消息和用户 ID 存储在数据框中?
感谢您的帮助。
据我所知,你的问题更多是关于 Python 和 Pandas 而不是 Telegram 和 Telethon。
from telethon.sync import TelegramClient
name = 'anon'
api_id = 'myAPI_ID'
api_hash = "myAPI_hash"
chat = 'chat_link'
async with TelegramClient(name, api_id, api_hash) as client:
async for message in client.iter_messages(chat):
print(message.sender_id, ':', message.text)
使用此代码,您将迭代 Telegram 聊天的消息,然后打印发送的 ID 和消息文本。
要将它们存储在变量中,您只需更改
print(message.sender_id, ':', message.text)
到
sender, text = message.sender_id, message.text
您可以将数据附加到列表,然后将其保存到 pandas 数据框。
将这一切结合在一起
import pandas as pd
from telethon.sync import TelegramClient
name = 'anon'
api_id = 'myAPI_ID'
api_hash = 'myAPI_hash'
chat = 'chat_link'
data = [] # stores all our data in the format SENDER_ID, MSG
async with TelegramClient(name, api_id, api_hash) as client:
async for message in client.iter_messages(chat):
data.append([message.sender_id, message.text])
df = pd.DataFrame(data, columns=['SENDER', 'MESSAGE']) # creates a new dataframe
df.to_csv('filename.csv', encoding='utf-8') # save to a CSV file
注意:在聊天中重复消息时注意 API 限制。