This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package main | |
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "net" | |
| "net/http" | |
| "os" | |
| "time" | |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| c.WithTransport(&http.Transport{ | |
| DialContext: (&net.Dialer{ | |
| Timeout: 60 * time.Second, | |
| KeepAlive: 30 * time.Second, | |
| DualStack: true, | |
| }).DialContext, | |
| MaxIdleConns: 100, | |
| IdleConnTimeout: 90 * time.Second, | |
| TLSHandshakeTimeout: 10 * time.Second, | |
| ExpectContinueTimeout: 1 * time.Second, |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| c.OnHTML("a.paging-controls__next.js-page-control", func(p *colly.HTMLElement) { | |
| nextPage := p.Request.AbsoluteURL(p.Attr("data-href")) | |
| c.Visit(nextPage) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import ( | |
| "encoding/json" | |
| "fmt" | |
| "os" | |
| "github.com/gocolly/colly" | |
| ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //We pass the data to Marshal | |
| content, err := json.Marshal(allProducts) | |
| //We need to handle someone the potential error | |
| if err != nil { | |
| fmt.Println(err.Error()) | |
| } | |
| //We write a new file passing the file name, data, and permission | |
| os.WriteFile("jack-shoes.json", content, 0644) | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| c.OnHTML("div.product-tile__content-wrapper", func(h *colly.HTMLElement) { | |
| products := products{ | |
| Name: h.ChildText("a.product-tile__name__link.js-product-tile-link"), | |
| Price: h.ChildText("em.value__price"), | |
| URL: h.ChildAttr("a.product-tile__name__link.js-product-tile-link", "href"), | |
| } | |
| fmt.Println(products) | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type products struct { | |
| Name, Price, URL string | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type products struct { | |
| Name string `json:"name"` | |
| Price string `json:"price"` | |
| URL string `json:"url"` | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| type products struct { | |
| Name string | |
| Price string | |
| URL string | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| c.OnHTML("div.product-tile__content-wrapper", func(h *colly.HTMLElement) { | |
| name := h.ChildText("a.product-tile__name__link.js-product-tile-link") | |
| price := h.ChildText("em.value__price") | |
| url := h.ChildAttr("a.product-tile__name__link.js-product-tile-link", "href") | |
| fmt.Println(name, price, url) | |
| }) |
NewerOlder