如何忽略打印”Max depth limit reached”的信息,使用go colly库。

huangapple go评论71阅读模式
英文:

how to ignore printing Max depth limit reached go colly

问题

我有一个使用Go语言编写的Colly爬虫,我正在尝试爬取多个网站。在我的终端上,它会打印很多这样的信息:

2023/05/30 02:22:56 达到最大深度限制
2023/05/30 02:22:56 达到最大深度限制
2023/05/30 02:22:56 达到最大深度限制
2023/05/30 02:22:56 达到最大深度限制
2023/05/30 02:22:56 达到最大深度限制
2023/05/30 02:22:56 达到最大深度限制
2023/05/30 02:22:56 达到最大深度限制

这使得我很难阅读我放置的一些打印信息。我想知道是否有办法在终端中忽略打印这些信息。谢谢。

英文:

i have a go colly crawler that i am trying to crawl many sites . on my terminal it prints a lot of :

2023/05/30 02:22:56 Max depth limit reached
2023/05/30 02:22:56 Max depth limit reached
2023/05/30 02:22:56 Max depth limit reached
2023/05/30 02:22:56 Max depth limit reached
2023/05/30 02:22:56 Max depth limit reached
2023/05/30 02:22:56 Max depth limit reached
2023/05/30 02:22:56 Max depth limit reached

it makes it hard for me to read some prints that i have placed . i wanted to know if there is any way to ignore printing this in termial . thanks

答案1

得分: 1

Max depth limit reachedcolly.ErrMaxDepth。你的项目中可能有类似这样的代码:

c := colly.NewCollector(colly.MaxDepth(5))

// ...

if err := c.Visit("http://go-colly.org/"); err != nil {
	log.Println(err)
}

如果你不想记录这个错误,可以添加一个简单的检查来排除它:

c := colly.NewCollector(colly.MaxDepth(5))

// ...

if err := c.Visit("http://go-colly.org/"); err != nil {
	// 只有当错误不是 ErrMaxDepth 时才记录错误。
	if err != colly.ErrMaxDepth {
		log.Println(err)
	}
}

另一种选择是将输出重定向到文件:

go run . 2>&1 >log.txt

或者将输出复制到文件并同时输出到标准输出使用 tee

go run . 2>&1 | tee log.txt
英文:

Max depth limit reached is colly.ErrMaxDepth. There must be codes like this in your project:

c := colly.NewCollector(colly.MaxDepth(5))

// ...

if err := c.Visit("http://go-colly.org/"); err != nil {
	log.Println(err)
}

If you don't want to log this error, add a simple check to exclude it:

c := colly.NewCollector(colly.MaxDepth(5))

// ...

if err := c.Visit("http://go-colly.org/"); err != nil {
	// log the error only when the error is not ErrMaxDepth.
	if err != colly.ErrMaxDepth {
		log.Println(err)
	}
}

Another option is to redirect the output to a file:

go run . 2>&1 >log.txt

or copy the output to a file and also to standard output with tee:

go run . 2>&1 | tee log.txt

huangapple
  • 本文由 发表于 2023年5月30日 06:53:50
  • 转载请务必保留本文链接:https://go.coder-hub.com/76360722.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定