-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
81 lines (68 loc) · 2.1 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
package main
import (
"encoding/json"
"fmt"
"log"
"os"
"strings"
"github.com/gocolly/colly"
)
type car struct {
Name string `json:"name"`
Price string `json:"price"`
AdUrl string `json:"urlad"`
PhotoUrl string `json:"urlphoto"`
}
func main() {
c := colly.NewCollector()
findNextPageOrEnd()
var cars []car
// Find and visit all links
c.OnHTML("div.search-row.swipable", func(e *colly.HTMLElement) {
car := car{
Name: e.ChildText("h2.title"),
Price: e.ChildText("span.price-no-decimals"),
AdUrl: "https://car.gr" + e.ChildAttr("a", "href"),
PhotoUrl: e.ChildAttr("img.thumbnail__image.swipable.tw-max-w-full.tw-h-auto", "src"),
}
cars = append(cars, car)
fmt.Println(cars)
content, err := json.Marshal(cars)
if err != nil {
log.Println(err.Error())
}
os.WriteFile("cars.json", content, 0755)
fmt.Println(len(cars))
})
c.OnHTML("[role=menuitem]", func(e *colly.HTMLElement) {
nextPage := e.Request.AbsoluteURL(e.Attr("href"))
// fmt.Println("goes to: " + e.Attr("href"))
if strings.Compare(e.Text, "»") > -1 {
fmt.Println(e.Text)
// fmt.Print(e.Attr("disabled"))
c.Visit(nextPage)
}
})
// c.OnRequest(func(r *colly.Request) {
// fmt.Println("Visiting", r.URL)
// })
c.Visit("https://www.car.gr/classifieds/cars/?condition=new&condition=used&distance=1000&engine_power-from=90&features=302&mileage-to=%3C125000&offer_type=sale&onlyprice=1&pg=1&postcode=56224&prefecture=7&price-to=%3C17500&price=%3E50®istration-from=%3E2012&rg=2&significant_damage=f&sort=pra/")
}
func findNextPageOrEnd() {
c := colly.NewCollector()
// var disabledNextPage string
c.OnHTML("div.tw-flex.tw-w-full", func(e *colly.HTMLElement) {
fmt.Println(e)
// disabledNextPage = e.ChildText("h5")
// // if disabledNextPage {
// // }
// // disabledNextPage = e.Text
// fmt.Println(disabledNextPage)
// stringPouPsaxnw := " Δεν βρέθηκαν αποτελέσματα για αυτά τα κριτήρια "
// apotelesma := strings.Compare(disabledNextPage, stringPouPsaxnw)
// if apotelesma > -1 {
os.Exit(3)
// }
})
// return disabledNextPage
}