-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
61 lines (39 loc) · 946 Bytes
/
Copy pathmain.go
File metadata and controls
61 lines (39 loc) · 946 Bytes
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
package main
import (
"encoding/json"
"fmt"
"os"
"github.com/gocolly/colly"
)
type item struct {
Name string `json:"name"`
Price string `json:"price"`
ImgUrl string `json:"imgurl"`
}
func main() {
c := colly.NewCollector(
colly.AllowedDomains("j2store.net"),
)
var items[] item
c.OnHTML(" div.col-sm-9 div[itemprop=itemListElement]", func(h *colly.HTMLElement) {
item:=item{
Name: h.ChildText("h2.product-title "),
Price:h.ChildText("div.sale-price"),
ImgUrl:h.ChildAttr("img","src"),
}
items = append(items, item)
})
c.OnHTML("[title=Next]",func(h *colly.HTMLElement) {
next_page:=h.Request.AbsoluteURL(h.Attr("href"))
c.Visit(next_page)
})
c.OnRequest(func (r *colly.Request) {
fmt.Println(r.URL.String())
})
c.Visit("https://j2store.net/demo/index.php/shop")
content,err:=json.Marshal(items)
if err!=nil{
fmt.Println(err.Error())
}
os.WriteFile("products.json",content,0666)
}