main.go 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "github.com/PuerkitoBio/goquery"
  7. )
  8. func main() {
  9. // Wordpress
  10. url := "https://addand.kr/shop/new-%ed%95%9c-%ea%b6%8c%ec%9c%bc%eb%a1%9c-%eb%81%9d%eb%82%98%eb%8a%94-%eb%85%b8%ec%85%98/" // Replace with the URL of your choice
  11. // Send an HTTP GET request to the URL
  12. response, err := http.Get(url)
  13. if err != nil {
  14. log.Fatal(err)
  15. }
  16. defer response.Body.Close()
  17. doc, err := goquery.NewDocumentFromReader(response.Body)
  18. if err != nil {
  19. log.Fatal(err)
  20. }
  21. // These will the value of PDP parsing structures
  22. cItemName := ".product_title"
  23. cShortDesc := ".woocommerce-product-details__short-description"
  24. cItemCategory := ".posted_in"
  25. cItemTags := ".tagged_as"
  26. cItemImages := ".woocommerce-product-gallery__image"
  27. // Use the Find method to select elements that match the css selector
  28. // doc.Find(cItemName).Each(func(index int, element *goquery.Selection) {
  29. // // Extract the text associated with the selected element
  30. // text := element.Text()
  31. // fmt.Printf("Text associated with %s: %s\n", cItemName, text)
  32. // })
  33. fmt.Println("ItemName: ", doc.Find(cItemName).First().Text())
  34. fmt.Println("ShortDesc: ", doc.Find(cShortDesc).First().Text())
  35. fmt.Println("ItemCategory: ", doc.Find(cItemCategory).First().Text())
  36. fmt.Println("ItemTags: ", doc.Find(cItemTags).First().Text())
  37. doc.Find(cItemImages).Each(func(index int, element *goquery.Selection) {
  38. href, _ := element.Attr("href")
  39. fmt.Printf("ItemImages: %s\n", href)
  40. })
  41. }