1. 程式人生 > >[Python] [爬蟲] 9.批量政府網站的招投標、中標資訊爬取和推送的自動化爬蟲——爬蟲日誌

[Python] [爬蟲] 9.批量政府網站的招投標、中標資訊爬取和推送的自動化爬蟲——爬蟲日誌

目錄

1.Intro

2.Source


1.Intro

檔名:spiderLog.py

模組名:爬蟲日誌

引用庫:

logging

功能:日誌寫入到文字,包含普通訊息、警告、錯誤、異常等,可以跟蹤爬蟲執行過程。

 

2.Source

#!/usr/bin/env Python
# -*- coding: utf-8 -*-
'''
# Author  : YSW
# Time    : 2018/6/6 14:05
# File    : spiderLog.py
# Version : 1.0
# Describe: 爬蟲日誌
# Update  :
'''

import logging

LOG_PATH = "SpiderLog.txt"
handler = logging.FileHandler(LOG_PATH)
handler.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s')
handler.setFormatter(formatter)
console = logging.StreamHandler()
console.setLevel(logging.INFO)

class SpiderLog(object):
    def __init__(self, log_location):
        self.logger = logging.getLogger(log_location)  # __name__
        self.logger.setLevel(level=logging.INFO)
        self.logger.addHandler(handler)
        self.logger.addHandler(console)

    def info(self, message_info):
        self.logger.info(message_info)

    def warning(self, message_warn):
        self.logger.warn(message_warn)

    def error(self, message_error):
        self.logger.error(message_error)

    def debug(self, message_debug):
        self.logger.debug(message_debug)

    def exception(self, message_exception):
        self.logger.exception(message_exception)