Hoj*_*olf 0 python telegram telethon
尝试运行Telethon 文档提供的基本代码时,我收到以下错误。我很困惑,因为我还没有建立一个循环。
RuntimeError:如果事件循环正在运行,则必须使用“async with”(即您在“async def”中)
我在 spyder 4.0.1 中使用 python 3.7.7
from telethon.sync import TelegramClient
from telethon import functions, types
def channel_info(username, api_id, api_hash):
with TelegramClient(username, api_id, api_hash,channel) as client:
result = client(functions.channels.GetFullChannelRequest(
channel=channel
))
return(result)
out = channel_info(username, api_id, api_hash)
Run Code Online (Sandbox Code Playgroud)
根据Telethon 文档的FAQ 部分。
我可以将 Anaconda/Spyder/IPython 与库一起使用吗?
是的,但是这些解释器隐式地运行 asyncio 事件循环,这会干扰 telethon.sync 魔法模块。如果使用它们,则不应导入同步。
所以避免使用sync
模块。
你可以尝试做这样的事情:
from telethon import TelegramClient, functions, types
from asyncio import run
API_ID= ...
API_HASH=" ... "
async def channel_info(username, api_id, api_hash):
async with TelegramClient('session', api_id, api_hash) as client:
result = await client(functions.channels.GetFullChannelRequest(
channel=username
))
return(result)
out = run(channel_info('durov', API_ID, API_HASH))
print(out)
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1591 次 |
最近记录: |