首頁  >  文章  >  後端開發  >  如何忽略列印達到最大深度限制 go colly

如何忽略列印達到最大深度限制 go colly

WBOY
WBOY轉載
2024-02-09 15:45:10656瀏覽

如何忽略打印达到最大深度限制 go colly

php小編草莓今天為大家介紹一個方法,可以幫助我們在使用go colly爬蟲框架時,忽略列印達到最大深度限制的問題。在爬取網頁資料的過程中,我們通常會遇到結構嵌套較深的情況,而colly框架預設的列印深度限制可能無法完整展示所有資料。透過對colly框架的調試選項進行設置,我們可以輕鬆解決這個問題,獲得更全面的數據展示。接下來,讓我們一起來了解具體的操作步驟吧!

問題內容

我有一個 go colly 爬蟲,我正在嘗試爬行許多網站。在我的終端上它印了很多:

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

這讓我很難閱讀我放置的一些印刷品。我想知道是否有任何方法可以忽略在終端機中列印此內容。謝謝

解決方法

達到最大深度限制colly.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 {
    // log the error only when the error is not errmaxdepth.
    if err != colly.errmaxdepth {
        log.println(err)
    }
}

另一個選項是將輸出重定向到檔案:

go run . 2>&1 >log.txt

或使用 tee 將輸出複製到檔案並複製到標準輸出:

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

以上是如何忽略列印達到最大深度限制 go colly的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:stackoverflow.com。如有侵權,請聯絡admin@php.cn刪除