-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
40 lines (29 loc) · 770 Bytes
/
main.go
File metadata and controls
40 lines (29 loc) · 770 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
package main
import (
"fmt"
"github.com/gocolly/colly/v2"
)
func main(){
c:= colly.NewCollector()
quoteCount := 0
// add simple call back to testt
c.OnRequest(func(r *colly.Request) {
fmt.Println("Visiting:", r.URL)
})
// response call back
c.OnResponse(func(r *colly.Response){
fmt.Println("got a respones! Status code:", r.StatusCode)
})
c.OnHTML("div.quote", func(e *colly.HTMLElement){
quoteCount++
quote := e.ChildText("span.text")
author := e.ChildText("small.author")
fmt.Printf("Quote #%d:\n", quoteCount)
fmt.Printf(" Text: %s\n", quote)
fmt.Printf(" Author: %s\n", author)
fmt.Println()
})
// vist website
c.Visit("https://quotes.toscrape.com/")
fmt.Printf("Scraping complete! Found %d quotes.\n", quoteCount)
}