在项目的settings.py文件中配置EXTENSIONS:
# Enable or disable extensions
# See https://doc.scrapy.org/en/latest/topics/extensions.html
EXTENSIONS = {
'scrapy.extensions.telnet.TelnetConsole': None,
'bankproduct.dispatch.BankProductDispatch': 599
}
钉玎机器人的使用:
class DingMessager(object):
logger = logging.getLogger(__name__)
robot_access_token = "e13799200d4637520acabc09f7ee38d8*******"
def _gen_text_msg(self, text):
msg_type = 'text'
msg = {"content": text}
return msg_type, msg
def _gen_markdown_msg(self, title, text):
msg_type = 'markdown'
markdown = {
"title": title,
"text": text
}
return msg_type, markdown
def _send_msg(self, url, access_token, body):
headers = {"Content-type": "application/json"}
resp = requests.post(url + access_token, headers=headers, data=body, verify=False)
self.logger.info(resp.text)
pass
# 钉玎机器人告警
def send_text_from_robot(self, access_token, text):
msg_type, msg = self._gen_text_msg(text)
body_dict = {
"msgtype": msg_type,
}
body_dict[msg_type] = msg
body = json.dumps(body_dict)
return self._send_msg("https://oapi.dingtalk.com/robot/send?access_token=", access_token, body)
pass
def send_markdown_from_robot(self, access_token, title, text):
msg_type, markdown = self._gen_markdown_msg(title, text)
body_dict = {
"msgtype": msg_type,
}
body_dict[msg_type] = markdown
body = json.dumps(body_dict)
return self._send_msg("https://oapi.dingtalk.com/robot/send?access_token=", access_token, body)
if __name__ == '__main__':
robot = DingMessager()
# robot.send_text_from_robot(robot.robot_access_token, "hello 123")
text = "* 爬虫脚本:%s\n" + "* 错误类型:%s\n" + "* 当前时间:%s\n"
text = text % (
"ceb", "测试异常信息", time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time())))
robot.send_markdown_from_robot(robot.robot_access_token, "bankproduct", text)
使用PyDispatcher
def __init__(self):
# 初始化钉玎告警次数
self.message_count = 0
dispatcher.connect(self.send_message, signal=signals.spider_error)
def send_message(self, error, spider, message):
"""
当spider出现error时发送邮件到邮箱
"""
if self.message_count < 1:
robot = DingMessager()
text = "* 爬虫脚本:%s\n" + "* 错误描述:%s\n" + "* 错误原因:%s\n" + "* 发生时间:%s\n"
text = text % (
spider.name,
message,
error,
time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))
)
robot.send_markdown_from_robot(robot.robot_access_token, "bankproduct", text)
self.message_count += 1
在有可能出现异常的地方使用:
dispatcher.send(signal=signals.spider_error, error=e, spider=self, message="推送数据异常")
参看文档:https://open-doc.dingtalk.com/microapp/serverapi2/qf2nxq
网友评论