Contents

这两天用go做了其他平台的爬虫程序,涉及到很多复杂的json处理,记录一下

json串demo

1
{"response":{"code":0,"msg":"success"},"data":{"page":"1","size":"1","total":"111","list":[{"Fchild_id":"0","Fchild_name":"","Fchild_uin":"1111","Fstatus":53,"Fstatus_cn":"aaaaa","Fcreate_time":"0000-00-00 00:00:00","Funbind_time":"0000-00-00 00:00:00","Ftime":"---","Fsource":2,"Fsource_cn":"aaaaaaa","Freason":"aaaaaa","telephone":"11111","head_url":"","introduction":"","primary_catalog":"aaaa","secondary_catalog":"aaaaa","homepage_url":"aaaaa","optimum_url":"aaaaaaa","remaining_times":5}]}}

获取到的json是字符串,所以相关操作是。先转换为json对象,然后在进行处理

我先转换为json对象,然后循环处理,将每条data里的list数据,一条一条写入到exlce表格中
贴出其中一段代码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
此处result变量为刚刚的json串demo
var result_json_data map[string]map[string]interface{}
result_json_data = make(map[string]map[string]interface{})
json.Unmarshal([]byte(result), &result_json_data)
list := result_json_data["data"]["list"].([]interface{})
fmt.Println("全部数据爬取完毕,正在生成excel表格... ...")
xlsx := excelize.NewFile()
// 创建一个新的工作表
index := xlsx.NewSheet("Sheet1")
var hang []string
hang = append(hang, "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S")
hang_num := 1
for _, v := range list {
for k2, v2 := range v.(map[string]interface{}) {
if k2 == "Fchild_id" {
xlsx.SetCellValue("Sheet1", "A"+strconv.Itoa(hang_num), v2)
} else if k2 == "Fchild_name" {
xlsx.SetCellValue("Sheet1", "B"+strconv.Itoa(hang_num), v2)
} else if k2 == "Fchild_uin" {
xlsx.SetCellValue("Sheet1", "C"+strconv.Itoa(hang_num), v2)
} else if k2 == "Fstatus" {
xlsx.SetCellValue("Sheet1", "D"+strconv.Itoa(hang_num), v2)
} else if k2 == "Fstatus_cn" {
xlsx.SetCellValue("Sheet1", "E"+strconv.Itoa(hang_num), v2)
} else if k2 == "Fcreate_time" {
xlsx.SetCellValue("Sheet1", "F"+strconv.Itoa(hang_num), v2)
} else if k2 == "Funbind_time" {
xlsx.SetCellValue("Sheet1", "G"+strconv.Itoa(hang_num), v2)
} else if k2 == "Ftime" {
xlsx.SetCellValue("Sheet1", "H"+strconv.Itoa(hang_num), v2)
} else if k2 == "Fsource" {
xlsx.SetCellValue("Sheet1", "I"+strconv.Itoa(hang_num), v2)
} else if k2 == "Fsource_cn" {
xlsx.SetCellValue("Sheet1", "J"+strconv.Itoa(hang_num), v2)
} else if k2 == "Freason" {
xlsx.SetCellValue("Sheet1", "K"+strconv.Itoa(hang_num), v2)
} else if k2 == "telephone" {
xlsx.SetCellValue("Sheet1", "L"+strconv.Itoa(hang_num), v2)
} else if k2 == "head_url" {
xlsx.SetCellValue("Sheet1", "M"+strconv.Itoa(hang_num), v2)
} else if k2 == "introduction" {
xlsx.SetCellValue("Sheet1", "N"+strconv.Itoa(hang_num), v2)
} else if k2 == "primary_catalog" {
xlsx.SetCellValue("Sheet1", "O"+strconv.Itoa(hang_num), v2)
} else if k2 == "secondary_catalog" {
xlsx.SetCellValue("Sheet1", "P"+strconv.Itoa(hang_num), v2)
} else if k2 == "homepage_url" {
xlsx.SetCellValue("Sheet1", "Q"+strconv.Itoa(hang_num), v2)
} else if k2 == "optimum_url" {
xlsx.SetCellValue("Sheet1", "R"+strconv.Itoa(hang_num), v2)
} else if k2 == "remaining_times" {
xlsx.SetCellValue("Sheet1", "S"+strconv.Itoa(hang_num), v2)
}
}
hang_num++
}
// 设置工作表的活动页
xlsx.SetActiveSheet(index)
// 保存文件到指定目录
timestamp := time.Now().Unix()
tm := time.Unix(timestamp, 0)
err := xlsx.SaveAs("./" + tm.Format("2006-01-02") + ".xlsx")
if err != nil {
fmt.Println(err)
}

Contents