将 colly 包输出文本添加到 golang 中的映射

我正在用 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


任何想法?


宝慕林4294392
浏览 143回答 1
1回答

收到一只叮咚

错误在地图分配中。管理如此嵌套的结构非常困难,但我找到了成功处理它的方法。让我展示代码:package mainimport (&nbsp; &nbsp; "encoding/json"&nbsp; &nbsp; "fmt"&nbsp; &nbsp; "strconv"&nbsp; &nbsp; "time"&nbsp; &nbsp; "github.com/gocolly/colly/v2")type contest struct{}func AtcoderFunc(contests map[string]map[string]map[string]string) {&nbsp; &nbsp; collector := colly.NewCollector(&nbsp; &nbsp; &nbsp; &nbsp; colly.AllowedDomains("atcoder.jp", "www.atcoder.jp"),&nbsp; &nbsp; )&nbsp; &nbsp; format := "2006-01-02 15:04:05-0700"&nbsp; &nbsp; loc, _ := time.LoadLocation("Asia/Calcutta")&nbsp; &nbsp; contests["UpcomingContest"] = make(map[string]map[string]string)&nbsp; &nbsp; for i := 1; i < 3; i++ {&nbsp; &nbsp; &nbsp; &nbsp; rawI := strconv.Itoa(i)&nbsp; &nbsp; &nbsp; &nbsp; contests["UpcomingContest"][rawI] = make(map[string]string)&nbsp; &nbsp; &nbsp; &nbsp; contestSelTime := fmt.Sprintf("#contest-table-upcoming&nbsp; div&nbsp; div&nbsp; table&nbsp; tbody&nbsp; tr:nth-child(%d)&nbsp; td:nth-child(1)&nbsp; a", i+1)&nbsp; &nbsp; &nbsp; &nbsp; contestSelName := fmt.Sprintf("#contest-table-upcoming&nbsp; div&nbsp; div&nbsp; table&nbsp; tbody&nbsp; tr:nth-child(%d)&nbsp; td:nth-child(2)", i)&nbsp; &nbsp; &nbsp; &nbsp; // for contest name&nbsp; &nbsp; &nbsp; &nbsp; collector.OnHTML(contestSelName, func(element *colly.HTMLElement) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contestName := element.ChildText("a")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contests["UpcomingContest"][rawI]["Name"] = contestName&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; // for contestTime&nbsp; &nbsp; &nbsp; &nbsp; collector.OnHTML(contestSelTime, func(element *colly.HTMLElement) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ContestStartTime := element.ChildText("time")&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; parsed_time, _ := time.Parse(format, ContestStartTime)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; IST_time := parsed_time.In(loc)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; contests["UpcomingContest"][rawI]["Time"] = fmt.Sprint(IST_time)&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; }&nbsp; &nbsp; collector.OnRequest(func(r *colly.Request) {&nbsp; &nbsp; &nbsp; &nbsp; fmt.Println("Visiting", r.URL.String())&nbsp; &nbsp; })&nbsp; &nbsp; collector.Visit("https://atcoder.jp/contests")}func main() {&nbsp; &nbsp; contests := make(map[string]map[string]map[string]map[string]string)&nbsp; &nbsp; contests["AtCoder"] = make(map[string]map[string]map[string]string)&nbsp; &nbsp; AtcoderFunc(contests["AtCoder"])&nbsp; &nbsp; data, _ := json.MarshalIndent(contests, "", "&nbsp; ")&nbsp; &nbsp; fmt.Println(string(data))}我或多或少保留了你的结构。除了解决这个问题之外,我通过更改一些名称并删除未使用的语句来稍微重构了您的示例。最后,我使用MarshalIndent函数美化了打印到终端上的 JSON 字符串。让我知道是否也适合你!
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Go