-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
158 lines (132 loc) · 3.2 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
package main
import (
"bufio"
"encoding/csv"
"fmt"
_ "go/printer"
"log"
"net/http"
"os"
"os/exec"
"strings"
"sync"
"github.com/PuerkitoBio/goquery"
)
func getHtml(url string) *http.Response {
response, err := http.Get(url)
checkErr(err)
if response.StatusCode > 400 {
fmt.Println("Status Code:", response.StatusCode)
}
if response.StatusCode != 200 {
log.Fatalf("status code error: %d %s", response.StatusCode, response.Status)
}
return response
}
func saveToCSV(result []string) {
file, error := os.Create("results.csv")
checkErr(error)
writer := csv.NewWriter(file)
writer.Write(result)
writer.Flush()
}
func scrapeHtml(doc *goquery.Document) []string {
var result []string
doc.Find(".product-wrapper").Each(func(index int, item *goquery.Selection) {
a := item.Find("h2 a")
price := item.Find(".price.small")
title := strings.TrimSpace(a.Text())
priceValue := strings.TrimSpace(price.Text())
fmt.Println("Titel:")
fmt.Printf("%+v \n", title)
fmt.Println("Preis:")
fmt.Printf("%+v \n", priceValue)
result = append(result, title, priceValue, "\n")
})
return result
}
func main() {
ClearTerminal()
PrintMenue()
for true {
ExecuteCommand()
}
}
func checkErr(err error) {
if err != nil {
fmt.Println(err)
}
}
func searchThroughWebsite(page int, command string, result *[]string, wg *sync.WaitGroup) {
defer wg.Done()
url := fmt.Sprintf("https://www.mediamarkt.ch/de/search.html?query=%s&searchProfile=onlineshop&channel=mmchde&page=%d", command, page)
response := getHtml(url)
defer response.Body.Close()
doc, err := goquery.NewDocumentFromReader(response.Body)
fmt.Println(&doc)
checkErr(err)
*result = append(*result, scrapeHtml(doc)...)
}
func loopOver(command string) []string {
var result []string
var wg sync.WaitGroup
wg.Add(5)
//Markenbezeichnungen in "command" generienen teilweise Unterseiten welche nicht gelesen werden können
go searchThroughWebsite(1, command, &result, &wg)
go searchThroughWebsite(2, command, &result, &wg)
go searchThroughWebsite(3, command, &result, &wg)
go searchThroughWebsite(4, command, &result, &wg)
go searchThroughWebsite(5, command, &result, &wg)
wg.Wait()
// Save results into a CSV File
saveToCSV(result)
return result
}
func PrintMenue() {
fmt.Println(`
##########################################
#******** Webscapper Mediamarkt **********
#********* Wähle eine Option *************
# 1. Suchen
# 2. Export (Beta)
#
# q. Beenden
##########################################
##########################################
##########################################
`)
}
func askForCommand() string {
reader := bufio.NewReader(os.Stdin)
response, err := reader.ReadString('\n')
if err != nil {
log.Fatal(err)
}
response = strings.TrimSpace(response)
return response
}
func ExecuteCommand() {
command := askForCommand()
parseCommand(command)
}
func ClearTerminal() {
c := exec.Command("clear")
c.Stdout = os.Stdout
c.Run()
}
func parseCommand(input string) {
switch {
case input == "1":
ClearTerminal()
fmt.Println("Suchwort eingeben:")
command := askForCommand()
loopOver(command)
break
case input == "2":
ClearTerminal()
// Sample für (Beta)Export
loopOver("Tastatur")
PrintMenue()
break
}
}