我刚刚运行了一个 Scrapy 蜘蛛,它花了大约 2 个小时来爬行(见下面的截图),但我忘记使用命令行选项 --logfile FILE
(参见 https://doc.scrapy.org/en/latest/topics/logging.html#command-line-options )将记录的输出保存到文件中。然而,我仍然想这样做,以追踪在抓取过程中发生的一些 ERROR
。
有没有办法“追溯”执行此操作而无需再重新抓取 2 小时?
最佳答案
有几个选项,但是由于 bash(和其他 shell)不记录输出,因此无法在过程完成后检索数据。
您可以尝试从 shell 复制它,但您只会得到最后几行,因为默认情况下 unix 终端具有有限的回滚历史记录。有一些增加回滚的方法,但通常不推荐这样做,请参阅相关内容:https://askubuntu.com/questions/385901/how-to-see-more-lines-in-the-terminal
相反,请确保显式启用日志记录到文件:
Linux 输出重定向:
# only sdout
scrapy crawl spider > output.log
# both stdout and stderr
scrapy crawl spider &> output.log
# to file and stdout
scrapy crawl spider 2>1 | tee output.log
Scrapy方式:
scrapy crawl spider -s LOG_FILE=output.log
scrapy crawl spider --logfile output.log
scrapy 支持用于覆盖设置的 -s
标志,并且有 LOG_FILE
设置可以在这种情况下使用,并且可以在项目中设置(例如 setting.py
文件)总是输出到日志文件。
关于linux - 在Linux中,日志输出产生后如何保存到文件中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43512232/