Repository navigation
Expand file tree
/
Copy pathmain.go
More file actions
71 lines (53 loc) · 1.43 KB
/
Copy pathmain.go
File metadata and controls
71 lines (53 loc) · 1.43 KB
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
package main
import (
"encoding/json"
"fmt"
"log"
"net/http"
"github.com/gocolly/colly"
"github.com/gorilla/mux"
)
type Product struct {
Title string `json:"title"`
Capital string `json:"capital"`
Population string `json:"population"`
Area string `json:"area"`
}
func scrapeWebsite() ([]Product, error) {
var products []Product
url := "https://www.scrapethissite.com/pages/simple/"
collector := colly.NewCollector()
collector.OnError(func(r *colly.Response, e error) {
fmt.Println("Error occurred!:", e)
})
collector.OnHTML(".col-md-4.country", func(e *colly.HTMLElement) {
product := Product{
Title: e.ChildText("h3"),
Capital: e.ChildText(".country-capital"),
Population: e.ChildText(".country-population"),
Area: e.ChildText(".country-area"),
}
products = append(products, product)
})
err := collector.Visit(url)
if err != nil {
return nil, err
}
collector.Wait()
return products, nil
}
func getScrapedDataHandler(w http.ResponseWriter, r *http.Request) {
data, err := scrapeWebsite()
if err != nil {
http.Error(w, "Oops, An error occured", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "application/json")
json.NewEncoder(w).Encode(data)
}
func main() {
r := mux.NewRouter()
r.HandleFunc("/api/scrape", getScrapedDataHandler).Methods("GET")
log.Println("Server running on port 8080")
log.Fatal(http.ListenAndServe(":8081", r))
}