-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
221 lines (192 loc) · 4.97 KB
/
main.go
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
package main
import (
"github.com/jinzhu/gorm"
_ "github.com/go-sql-driver/mysql"
"os"
"log"
"github.com/PuerkitoBio/goquery"
"time"
"strconv"
"net/url"
"io/ioutil"
"net/http"
"encoding/json"
)
type Address struct {
gorm.Model
AreaId int `json:"area_id,string"`
AreaName string `json:"area_name"`
ParentId int `json:"parent_id,string"`
Sort int `json:"sort,string"`
}
type Brand struct {
gorm.Model
Name string `json:"name"`
Image string `json:"image"`
Address string `json:"address"`
Trade string `json:"trade"`
}
func init() {
log.SetFlags(log.Llongfile | log.Lmicroseconds | log.Llongfile)
}
func main2() {
// http://yske.org/index.php?m=union&c=union&a=getArea
// areaId:130000
resp, err := http.PostForm("http://yske.org/index.php?m=union&c=union&a=getArea", url.Values{"areaId": {"130000"}})
if err != nil {
log.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
os.Exit(1)
}
var cities []Address
log.Println(string(body))
err = json.Unmarshal(body, &cities)
if err != nil {
log.Println(err)
os.Exit(1)
}
log.Println(cities)
}
func main() {
db, err := gorm.Open("mysql", "root:root@tcp(localhost:33062)/yske?charset=utf8&parseTime=True&loc=Local")
if err != nil {
log.Println(err)
os.Exit(1)
}
err = db.DB().Ping()
if err != nil {
log.Println(err)
os.Exit(1)
}
doc, err := goquery.NewDocument("http://yske.org/index.php?m=union&c=union&a=index&page=1")
time.Sleep(2 * time.Second)
if err != nil {
log.Println(err)
os.Exit(1)
}
li := doc.Find("#province option")
log.Println(li.Length())
li.Each(func(i int, s *goquery.Selection) {
areaId, _ := s.Attr("value")
areaName := s.Text()
log.Println(areaId, areaName)
aid, _ := strconv.Atoi(areaId)
address := Address{AreaId: aid, AreaName: areaName, Sort: 100 + i, ParentId: 0}
b := db.NewRecord(address)
if b {
log.Println("OK")
// 获取省下的市区
fetchCity(db, areaId)
}
db.Create(&address)
})
}
func fetchCity(db *gorm.DB, areaId string) {
// http://yske.org/index.php?m=union&c=union&a=getArea
// areaId:130000
log.Println(areaId)
resp, err := http.PostForm("http://yske.org/index.php?m=union&c=union&a=getArea", url.Values{"areaId": {areaId}})
time.Sleep(2 * time.Second)
if err != nil {
log.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
os.Exit(1)
}
var cities []Address
//log.Println(string(body))
err = json.Unmarshal(body, &cities)
if err != nil {
log.Println(err)
os.Exit(1)
}
//log.Println(cities)
for i, v := range cities {
address := Address{AreaId: v.AreaId, AreaName: v.AreaName, Sort: v.Sort + i + i, ParentId: v.ParentId}
b := db.NewRecord(address)
if b {
log.Println("OK")
// 获取市下的市区县市
fetchArea(db, strconv.Itoa(v.AreaId))
}
db.Create(&address)
}
}
func fetchArea(db *gorm.DB, areaId string) {
log.Println(areaId)
resp, err := http.PostForm("http://yske.org/index.php?m=union&c=union&a=getArea", url.Values{"areaId": {areaId}})
time.Sleep(2 * time.Second)
if err != nil {
log.Println(err)
os.Exit(1)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
log.Println(err)
os.Exit(1)
}
var cities []Address
//log.Println(string(body))
err = json.Unmarshal(body, &cities)
if err != nil {
log.Println(err)
os.Exit(1)
}
//log.Println(cities)
for i, v := range cities {
address := Address{AreaId: v.AreaId, AreaName: v.AreaName, Sort: v.Sort + i + i, ParentId: v.ParentId}
b := db.NewRecord(address)
if b {
log.Println("OK")
}
db.Create(&address)
}
}
func main1() {
db, err := gorm.Open("mysql", "root:root@tcp(localhost:33062)/yske?charset=utf8&parseTime=True&loc=Local")
if err != nil {
log.Println(err)
os.Exit(1)
}
err = db.DB().Ping()
if err != nil {
log.Println(err)
os.Exit(1)
}
db.AutoMigrate(&Address{}, &Brand{})
// http://yske.org/index.php?m=union&c=union&a=index&page=1
pageTotal := 123
for counter := 1; counter <= pageTotal; counter++ {
doc, err := goquery.NewDocument("http://yske.org/index.php?m=union&c=union&a=index&page=" + strconv.Itoa(counter))
time.Sleep(2 * time.Second)
if err != nil {
log.Println(err)
os.Exit(1)
}
li := doc.Find(".sjalllist>ul li")
log.Println(li.Length())
li.Each(func(i int, s *goquery.Selection) {
image, _ := s.Find("img").Attr("src")
trade := s.Find("span").Text()
name := s.Find(".sjinfo a").Text()
address := s.Find(".sjinfo p").Last().Find("i").Text()
log.Println(image, trade, name, address)
brand := Brand{Image: image, Trade: trade, Name: name, Address: address}
b := db.NewRecord(brand)
if b {
log.Println("OK")
}
db.Create(&brand)
})
}
}