亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

將 colly 包輸出文本添加到 golang 中的映射

將 colly 包輸出文本添加到 golang 中的映射

Go
寶慕林4294392 2023-03-15 14:52:59
我正在用 colly 包制作一個網絡抓取工具,它從一個網站收集ContestName和ContestTime制作一個 json 文件。所以我喜歡這個    Contests := make(map[string]map[string]map[string]map[string]string)        Contests["AtCoder"] = make(map[string]map[string]map[string]string)    Contests["AtCoder"]["FutureContests"] = make(map[string]map[string]string)    AtcoderFunc(Contests).................code..........func AtcoderFunc(Contests map[string]map[string]map[string]map[string]string) {    collector := colly.NewCollector(        colly.AllowedDomains("atcoder.jp", "www.atcoder.jp"),    )    // loc, _ := time.LoadLocation("Asia/Calcutta")    // format := "2006-01-02 15:04:05"    // var i int    format := "2006-01-02 15:04:05-0700"    loc, _ := time.LoadLocation("Asia/Calcutta")    for i := 1; i < 10; i++ {        ContestSelTime := fmt.Sprintf("#contest-table-upcoming  div  div  table  tbody  tr:nth-child(%d)  td:nth-child(1)  a", i+1)        ContestSelName := fmt.Sprintf("#contest-table-upcoming  div  div  table  tbody  tr:nth-child(%d)  td:nth-child(2)", i)        // for contest name        collector.OnHTML(ContestSelName, func(element *colly.HTMLElement) {            ContestName := element.ChildText("a")            fmt.Printf("%T \n", ContestName)            fmt.Println(ContestName) // instead of printing i want to add it to the Contests["AtCoder"]["FutureContests"] map and print like json                     })        // for contestTime        collector.OnHTML(ContestSelTime, func(element *colly.HTMLElement) {            ContestStartTime := element.ChildText("time")            parsed_time, _ := time.Parse(format, ContestStartTime)            IST_time := parsed_time.In(loc)            fmt.Println("Time in IST", IST_time) // instead of printing i want to add it to the Contests["AtCoder"]["FutureContests"] map.        })    }但它給出了錯誤cannot use (map[string]string literal) (value of type map[string]string) as map[string]map[string]string value in assignment任何想法?
查看完整描述

1 回答

?
收到一只叮咚

TA貢獻1821條經驗 獲得超5個贊

錯誤在地圖分配中。管理如此嵌套的結構非常困難,但我找到了成功處理它的方法。讓我展示代碼:


package main


import (

    "encoding/json"

    "fmt"

    "strconv"

    "time"


    "github.com/gocolly/colly/v2"

)


type contest struct{}


func AtcoderFunc(contests map[string]map[string]map[string]string) {

    collector := colly.NewCollector(

        colly.AllowedDomains("atcoder.jp", "www.atcoder.jp"),

    )


    format := "2006-01-02 15:04:05-0700"

    loc, _ := time.LoadLocation("Asia/Calcutta")


    contests["UpcomingContest"] = make(map[string]map[string]string)


    for i := 1; i < 3; i++ {

        rawI := strconv.Itoa(i)

        contests["UpcomingContest"][rawI] = make(map[string]string)


        contestSelTime := fmt.Sprintf("#contest-table-upcoming  div  div  table  tbody  tr:nth-child(%d)  td:nth-child(1)  a", i+1)

        contestSelName := fmt.Sprintf("#contest-table-upcoming  div  div  table  tbody  tr:nth-child(%d)  td:nth-child(2)", i)


        // for contest name

        collector.OnHTML(contestSelName, func(element *colly.HTMLElement) {

            contestName := element.ChildText("a")

            contests["UpcomingContest"][rawI]["Name"] = contestName

        })


        // for contestTime

        collector.OnHTML(contestSelTime, func(element *colly.HTMLElement) {

            ContestStartTime := element.ChildText("time")

            parsed_time, _ := time.Parse(format, ContestStartTime)

            IST_time := parsed_time.In(loc)

            contests["UpcomingContest"][rawI]["Time"] = fmt.Sprint(IST_time)

        })

    }


    collector.OnRequest(func(r *colly.Request) {

        fmt.Println("Visiting", r.URL.String())

    })


    collector.Visit("https://atcoder.jp/contests")

}


func main() {

    contests := make(map[string]map[string]map[string]map[string]string)

    contests["AtCoder"] = make(map[string]map[string]map[string]string)


    AtcoderFunc(contests["AtCoder"])


    data, _ := json.MarshalIndent(contests, "", "  ")

    fmt.Println(string(data))

}

我或多或少保留了你的結構。除了解決這個問題之外,我通過更改一些名稱并刪除未使用的語句來稍微重構了您的示例。最后,我使用MarshalIndent函數美化了打印到終端上的 JSON 字符串。

讓我知道是否也適合你!


查看完整回答
反對 回復 2023-03-15
  • 1 回答
  • 0 關注
  • 98 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號