前言
以下使用 Go 語言的 Colly 爬蟲框架,實作一個每隔 5 秒檢查商品庫存的通知程式。
做法
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
| package main
import ( "log" "net/smtp" "os" "strings" "time"
"github.com/gocolly/colly"
_ "github.com/joho/godotenv/autoload" )
const ( target = "https://helium.com.tw/collections/frontpage/products/2nd-copy-of-rak-hotspot-miner-donot-delete" )
func main() { for range time.Tick(5 * time.Second) { c := colly.NewCollector() c.OnHTML("#product-inventory span", func(e *colly.HTMLElement) { availability := strings.TrimSpace(e.Text) log.Printf("Availability: %s", availability) if strings.ToUpper(availability) == strings.ToUpper("Out of stock") { return } }) c.Visit(target) } }
|