【发布时间】:2020-08-01 11:32:52
【问题描述】:
我想编写一个 Telegram 机器人,它可以应要求提供 Scrapy 统计信息。 我的尝试大部分都有效,唯一的问题是强制关闭蜘蛛(显然)不会停止机器人。
所以我有两个问题:
- 我的一般方法是否正确?
- 是否可以在强制关闭爬虫的情况下关闭机器人?
这里是相关的类:
class TelegramBot(object):
telegram_token = telegram_credentials.token
@classmethod
def from_crawler(cls, crawler):
return cls(crawler)
def __init__(self, crawler):
self.crawler = crawler
cs = crawler.signals
cs.connect(self._spider_closed, signal=signals.spider_closed)
"""Start the bot."""
# Create the Updater and pass it your bot's token.
# Make sure to set use_context=True to use the new context based callbacks
# Post version 12 this will no longer be necessary
self.updater = Updater(self.telegram_token, use_context=True)
# Get the dispatcher to register handlers
dp = self.updater.dispatcher
# on different commands - answer in Telegram
dp.add_handler(CommandHandler("stats", self.stats))
# Start the Bot
self.updater.start_polling()
def _spider_closed(self, spider, reason):
# Stop the Bot
self.updater.stop()
def stats(self, update, context):
# Send a message with the stats
msg = (
"Spider "
+ self.crawler.spider.name
+ " stats: "
+ str(self.crawler.stats.get_stats())
)
update.message.reply_text(msg)
在这里你可以找到我在 Scrapy 教程引用蜘蛛 https://github.com/jtommi/scrapy_telegram-bot_example/blob/master/tutorial/tutorial/telegram-bot.py 中的完整代码
我的代码是一个组合
- “Learning Scrapy”一书中的延迟扩展https://github.com/scalingexcellence/scrapybook/blob/master/ch09/properties/properties/latencies.py
- 来自 python-telegram-bot 库 https://github.com/python-telegram-bot/python-telegram-bot/blob/master/examples/echobot.py 的 echobot 示例
- 关于统计收集的官方scrapy文档https://docs.scrapy.org/en/latest/topics/stats.html
【问题讨论】:
标签: python-3.x scrapy twisted python-telegram-bot