我正在尝试实现Spidermon来监视scrapyd:
https://github.com/scrapinghub/spidermon/blob/master/spidermon/contrib/scrapy/monitors.py https://spidermon.readthedocs.io/en/latest/monitors.html#is-there-a-basic-scrapy-suite-ready-to-use
当前,它监视最小数量的项目和最大数量的错误。如果发生错误,它将通过电报发送一条消息。不知何故,文档一定是错误的,或者是我的实现,因为错误或项目的数量不会包含在消息中:
from spidermon import Monitor, MonitorSuite, monitors
from spidermon.contrib.actions.telegram.notifiers import SendTelegramMessageSpiderFinished
from spidermon.contrib.monitors.mixins import StatsMonitorMixin
@monitors.name('Item count')
class ItemCountMonitor(Monitor):
@monitors.name('Minimum number of items')
def test_minimum_number_of_items(self):
item_extracted = getattr(
self.data.stats, 'item_scraped_count', 0)
minimum_threshold = 500
msg = 'Extracted less than {} items'.format(
minimum_threshold)
self.assertTrue(
item_extracted >= minimum_threshold, msg=msg
)
@monitors.name("Error Count Monitor")
class ErrorCountMonitor(Monitor):
"""Check for errors in the spider log.
You can configure the expected number of ERROR log messages using
``SPIDERMON_MAX_ERRORS``. The default is ``0``."""
@monitors.name("Should not have any errors")
def test_max_errors_in_log(self):
errors_threshold = self.crawler.settings.getint(SPIDERMON_MAX_ERRORS, 0)
no_of_errors = self.stats.get("log_count/ERROR", 0)
msg = "Found {} errors in log, maximum expected is " "{}".format(
no_of_errors, errors_threshold
)
self.assertTrue(no_of_errors <= errors_threshold, msg=msg)
class SpiderCloseMonitorSuite(MonitorSuite):
monitors = [
ItemCountMonitor,
# ItemValidationMonitor,
ErrorCountMonitor
]
monitors_failed_actions = [
SendTelegramMessageSpiderFinished,
]
如何在电报通知中包含味精文本?
答案 0 :(得分:0)
您可以在settings.py
或搜寻器的设置中设置两个选项。
要仅包含您要的错误消息,可以将SPIDERMON_TELEGRAM_NOTIFIER_INCLUDE_ERROR_MESSAGES
设置为True
。
如果在没有错误的情况下还希望获得更多详细消息,则可以添加SPIDERMON_TELEGRAM_NOTIFIER_INCLUDE_OK_MESSAGES
并将其设置为True
。
我找不到与此相关的任何文档,因此我必须将您指向源代码:SendTelegramMessageSpiderFinished
在您的settings.py
中添加以下内容应该说服message template为您提供完整的输出:
SPIDERMON_TELEGRAM_NOTIFIER_INCLUDE_OK_MESSAGES = True
SPIDERMON_TELEGRAM_NOTIFIER_INCLUDE_ERROR_MESSAGES = True