我正在用colly包制作一个web scraper,它从一个网站收集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.
})
}
collector.OnRequest(func(request *colly.Request) {
fmt.Println("Visiting", request.URL.String())
})
collector.Visit("https://atcoder.jp/contests")
}
有什么想法吗?我试着像这样给地图添加值
Contests["AtCoder"]["FutureContests"] = map[string]string{
"Name": string(ContestName),
}
我想让json像这样
{
"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
任何想法?
地图分配错误。管理一个如此嵌套的结构是相当困难的,但我找到了一个成功处理它的方法。下面是代码:
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字符串。
让我知道是否也适用于你!