goh*_*goh 17 python web-crawler scrapy
我决定使用Python日志记录模块,因为Twisted on std error生成的消息太长了,我希望将INFO有意义的消息(例如由生成的消息StatsCollector写入单独的日志文件)同时保持屏幕消息.
 from twisted.python import log
     import logging
     logging.basicConfig(level=logging.INFO, filemode='w', filename='buyerlog.txt')
     observer = log.PythonLoggingObserver()
     observer.start()
好吧,这很好,我收到了我的消息,但缺点是我不知道消息是由哪个蜘蛛生成的!这是我的日志文件,"twisted"显示为%(name)s:
 INFO:twisted:Log opened.
  2 INFO:twisted:Scrapy 0.12.0.2543 started (bot: property)
  3 INFO:twisted:scrapy.telnet.TelnetConsole starting on 6023
  4 INFO:twisted:scrapy.webservice.WebService starting on 6080
  5 INFO:twisted:Spider opened
  6 INFO:twisted:Spider opened
  7 INFO:twisted:Received SIGINT, shutting down gracefully. Send again to force unclean shutdown
  8 INFO:twisted:Closing spider (shutdown)
  9 INFO:twisted:Closing spider (shutdown)
 10 INFO:twisted:Dumping spider stats:
 11 {'downloader/exception_count': 3,
 12  'downloader/exception_type_count/scrapy.exceptions.IgnoreRequest': 3,
 13  'downloader/request_bytes': 9973,
与标准错误扭曲产生的消息相比:
2011-12-16 17:34:56+0800 [expats] DEBUG: number of rules: 4
2011-12-16 17:34:56+0800 [scrapy] DEBUG: Telnet console listening on 0.0.0.0:6023
2011-12-16 17:34:56+0800 [scrapy] DEBUG: Web service listening on 0.0.0.0:6080
2011-12-16 17:34:56+0800 [iproperty] INFO: Spider opened
2011-12-16 17:34:56+0800 [iproperty] DEBUG: Redirecting (301) to <GET http://www.iproperty.com.sg/> from <GET http://iproperty.com.sg>
2011-12-16 17:34:57+0800 [iproperty] DEBUG: Crawled (200) <
我已经尝试了%(name)s,%(module)s等等,但我似乎无法显示蜘蛛名称.有谁知道答案?
编辑:使用LOG_FILE和LOG_LEVEL设置中的问题是较低级别的消息将不会显示在std错误上.
Aco*_*orn 23
import logging
from scrapy.log import ScrapyFileLogObserver
logfile = open('testlog.log', 'w')
log_observer = ScrapyFileLogObserver(logfile, level=logging.DEBUG)
log_observer.start()
我很高兴你问过这个问题,我一直想自己这么做.
Ale*_*nko 17
使用以下方法重定向输出非常容易: scrapy some-scrapy's-args 2>&1 | tee -a logname
这样,scrapy输出到stdout和stderr的所有内容都将被重定向到一个logname文件,并且也会被引导到屏幕上.
对于在阅读当前文档版本之前来到这里的所有人:
import logging
from scrapy.utils.log import configure_logging
configure_logging(install_root_handler=False)
logging.basicConfig(
    filename='log.txt',
    filemode = 'a',
    format='%(levelname)s: %(message)s',
    level=logging.DEBUG
)
我知道这是旧的,但它是一个非常有用的帖子,因为该类仍未在Scrapy文档中正确记录.此外,我们可以跳过导入日志记录并直接使用scrapy日志.谢谢大家!
from scrapy import log
logfile = open('testlog.log', 'a')
log_observer = log.ScrapyFileLogObserver(logfile, level=log.DEBUG)
log_observer.start()
正如 Scrapy 官方文档所说:
Scrapy 使用 Python 的内置日志系统进行事件日志记录。
所以你可以像普通的 Python 脚本一样配置你的记录器。
首先,您必须导入日志记录模块:
import logging
您可以将此行添加到您的蜘蛛:
logging.getLogger().addHandler(logging.StreamHandler())
它添加了一个流处理程序来记录到控制台。
之后,您必须配置日志文件路径。
添加一个custom_settings包含蜘蛛指定设置的 dict 名称:
custom_settings = {
     'LOG_FILE': 'my_log.log',
     'LOG_LEVEL': 'INFO',
     ... # you can add more settings
 }
整个班级看起来像:
import logging
class AbcSpider(scrapy.Spider):
    name: str = 'abc_spider'
    start_urls = ['you_url']
    custom_settings = {
         'LOG_FILE': 'my_log.log',
         'LOG_LEVEL': 'INFO',
         ... # you can add more settings
     }
     logging.getLogger().addHandler(logging.StreamHandler())
     def parse(self, response):
        pass
| 归档时间: | 
 | 
| 查看次数: | 11303 次 | 
| 最近记录: |