configparser 中的列表作为参数传递给 telethon 错误

发布于 2025-01-13 05:48:57 字数 1202 浏览 0 评论 0原文

我遇到了一个小问题,让我陷入了从电报聊天中收集数据的代码小脚本的困境。首先,我将显示我的代码和配置文件:

Config.ini:

[account]
api_id = xxxx
api_hash = xxxx

[parser]
channels_to_parse = [-xxxx,-xxxx]

Run.py

import configparser

import asyncio
import time
from telethon import events
from telethon import TelegramClient
from telethon.tl import functions, types
from datetime import datetime


#load config file
config = configparser.ConfigParser()
config.read('config.ini', encoding="utf-8")

#telethon init
client = TelegramClient('sess', config.get("account", 'api_id'), config.get("account", 'api_hash'))
client.start()

#main cycle
@client.on(events.NewMessage(chats=config.get('parser' , 'channels_to_parse')))
async def main(event):
    #some code...
            
client.run_until_disconnected()

主要问题来自包含电视节目参数的字符串,该参数指向我从中收集数据的聊天ID:

ValueError: Cannot find any entity corresponding to "[-xxxx]"

当我手动传递参数时,没有configparser:

@client.on(events.NewMessage(chats = [-xxxx, -xxxx]))

一切正常出色地。所以我认为这个问题与 configparser 或 configparser 参数有关。我检查了 configparser 文档,没有找到任何可以帮助我的内容。

我已经尝试使用频道名称而不是 ID。也许谁可以解释我做错了什么。

I got a little problem that make stuck me at code small script that collect data from telegram chats. For first i will show my code and config file:

Config.ini:

[account]
api_id = xxxx
api_hash = xxxx

[parser]
channels_to_parse = [-xxxx,-xxxx]

Run.py

import configparser

import asyncio
import time
from telethon import events
from telethon import TelegramClient
from telethon.tl import functions, types
from datetime import datetime


#load config file
config = configparser.ConfigParser()
config.read('config.ini', encoding="utf-8")

#telethon init
client = TelegramClient('sess', config.get("account", 'api_id'), config.get("account", 'api_hash'))
client.start()

#main cycle
@client.on(events.NewMessage(chats=config.get('parser' , 'channels_to_parse')))
async def main(event):
    #some code...
            
client.run_until_disconnected()

The main problem goes from string that contains arguments for telethon that points chats IDs from which i collecting data:

ValueError: Cannot find any entity corresponding to "[-xxxx]"

When i passing arguments manually, without configparser:

@client.on(events.NewMessage(chats = [-xxxx, -xxxx]))

Everything works well. So i think that issue related to configparser or configparser parameters. I checked configparser docs and didn't find anything that can help me.

I already tried to use channels name instead IDs. Maybe who's can explain me what i do wrong.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

病女 2025-01-20 05:48:57

我目前无法检查这一点,但我知道 configparser 返回 str 类型,并且您需要一个列表。但我可能是错的(

更新

我检查了一下,我是对的!

config.ini:

[parser]
channels_to_parse = -xxxx -xxxx

file.py:

parser = config.get('parser', 'channels_to_parse') # type str
chats = [int(i) for i in parser.split()] # type list
@client.on(events.NewMessage(chats=chats))

I can't check this at the moment, but I have an idea that configparser returns the str type and you need a list. But I could be wrong(

UPDATE

I checked it out and I was right!

config.ini:

[parser]
channels_to_parse = -xxxx -xxxx

file.py:

parser = config.get('parser', 'channels_to_parse') # type str
chats = [int(i) for i in parser.split()] # type list
@client.on(events.NewMessage(chats=chats))
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文