首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加colly包输出文本到高丽地图中

添加colly包输出文本到高丽地图中
EN

Stack Overflow用户
提问于 2022-11-15 12:26:14
回答 1查看 24关注 0票数 3

我用collects制作了一个网络刮刀,它从一个网站收集ContestNameContestTime,并制作一个json文件。

所以我确实喜欢这个

代码语言:javascript
复制
    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.
        })

    }

    collector.OnRequest(func(request *colly.Request) {
        fmt.Println("Visiting", request.URL.String())
    })

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

}

有什么主意吗?我试着像这样把值添加到地图上

代码语言:javascript
复制
            Contests["AtCoder"]["FutureContests"] = map[string]string{
                "Name": string(ContestName),
            }

我想让json像这样

代码语言:javascript
复制
{
  "AtCoder": {
    "FutureContests": {
      "1": {
        "Name": "Contest name",
        "Start": "time here"
      },
      "2": {
        "Name": "Contest name",
        "Start": "time here"
      }
    }
  }
}

但是它给出了错误cannot use (map[string]string literal) (value of type map[string]string) as map[string]map[string]string value in assignment

知道吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-16 09:06:19

错误在地图分配中。很难管理一个如此嵌套的结构,但我找到了一种成功处理它的方法。让我介绍一下守则:

代码语言:javascript
复制
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字符串。

让我知道,如果也是你的工作!

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74445600

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档