-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathmain.go
48 lines (37 loc) · 842 Bytes
/
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
// 爬取豆瓣电影 TOP250
package main
import (
"log"
"strings"
"github.com/PuerkitoBio/goquery"
"github.com/go-crawler/douban-movie/model"
"github.com/go-crawler/douban-movie/parse"
)
var (
BaseUrl = "https://movie.douban.com/top250"
)
// 新增数据
func Add(movies []parse.DoubanMovie) {
for index, movie := range movies {
if err := model.DB.Create(&movie).Error; err != nil {
log.Printf("db.Create index: %s, err : %v", index, err)
}
}
}
// 开始爬取
func Start() {
var movies []parse.DoubanMovie
pages := parse.GetPages(BaseUrl)
for _, page := range pages {
doc, err := goquery.NewDocument(strings.Join([]string{BaseUrl, page.Url}, ""))
if err != nil {
log.Println(err)
}
movies = append(movies, parse.ParseMovies(doc)...)
}
Add(movies)
}
func main() {
Start()
defer model.DB.Close()
}