configparser 中的列表作为参数传递给 telethon 错误
我遇到了一个小问题,让我陷入了从电报聊天中收集数据的代码小脚本的困境。首先,我将显示我的代码和配置文件:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我目前无法检查这一点,但我知道 configparser 返回 str 类型,并且您需要一个列表。但我可能是错的(
更新
我检查了一下,我是对的!
config.ini:
file.py:
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:
file.py: