main.go 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. package main
  2. import (
  3. "fmt"
  4. "log"
  5. "net/http"
  6. "regexp"
  7. "github.com/PuerkitoBio/goquery"
  8. )
  9. func main() {
  10. // Wordpress
  11. // 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/"
  12. url := "https://seoulknit.com/shop/collar-half-sleeved-top_blue/"
  13. // Send an HTTP GET request to the URL
  14. response, err := http.Get(url)
  15. if err != nil {
  16. log.Fatal(err)
  17. }
  18. defer response.Body.Close()
  19. doc, err := goquery.NewDocumentFromReader(response.Body)
  20. if err != nil {
  21. log.Fatal(err)
  22. }
  23. // These will the value of PDP parsing structures
  24. cItemName := ".product_title"
  25. cShortDesc := ".woocommerce-product-details__short-description"
  26. cItemCategory := ".posted_in"
  27. cItemTags := ".tagged_as"
  28. cItemImages := ".woocommerce-product-gallery__image img"
  29. cItemTextDesc := "div.woocommerce-tabs"
  30. // cItemOptions := "select#rating option"
  31. cItemPrice := ".summary>.price .woocommerce-Price-amount.amount"
  32. cEmail := "footer"
  33. cRating := ".star-rating .rating"
  34. cAuthor := ".product-brand a"
  35. cVideo := "iframe"
  36. // Use the Find method to select elements that match the css selector
  37. // doc.Find(cItemName).Each(func(index int, element *goquery.Selection) {
  38. // // Extract the text associated with the selected element
  39. // text := element.Text()
  40. // fmt.Printf("Text associated with %s: %s\n", cItemName, text)
  41. // })
  42. fmt.Println("ItemName: ", doc.Find(cItemName).First().Text())
  43. fmt.Println("ShortDesc: ", doc.Find(cShortDesc).First().Text())
  44. fmt.Println("ItemCategory: ", doc.Find(cItemCategory).First().Text())
  45. fmt.Println("ItemTags: ", doc.Find(cItemTags).First().Text())
  46. fmt.Println("TextDesc: ", doc.Find(cItemTextDesc).First().Text())
  47. fmt.Println("ItemPice: ", doc.Find(cItemPrice).First().Text())
  48. fmt.Println("Email: ", findEmail(doc.Find(cEmail).First().Text(), ""))
  49. fmt.Println("Rating : ", doc.Find(cRating).First().Text())
  50. doc.Find("meta").Each(func(i int, s *goquery.Selection) {
  51. v, _ := s.Attr("property")
  52. fmt.Println(v, " : ", s.AttrOr("content", ""))
  53. // v, _ := s.Attr("property")
  54. // if v == "og:description" {
  55. // fmt.Println("og:description : ", s.AttrOr("content", ""))
  56. // } else if v == "og:title" {
  57. // fmt.Println("og:title : ", s.AttrOr("content", ""))
  58. // } else if v == "og:image" {
  59. // fmt.Println("og:image : ", s.AttrOr("content", ""))
  60. // } else if v == "product:price:currency" {
  61. // fmt.Println("currency : ", s.AttrOr("content", ""))
  62. // }
  63. })
  64. fmt.Println("Author : ")
  65. doc.Find(cAuthor).Each(func(index int, element *goquery.Selection) {
  66. link, _ := element.Attr("href")
  67. fmt.Println("====================")
  68. fmt.Println("AuthorName: ", element.Text())
  69. fmt.Printf("AuthorLink: %s\n", link)
  70. })
  71. vdos := []string{}
  72. doc.Find(cVideo).Each(func(index int, element *goquery.Selection) {
  73. embed, _ := element.Attr("src")
  74. vdos = append(vdos, embed)
  75. })
  76. fmt.Printf("ItemVideos: %s\n", vdos)
  77. imgs := []string{}
  78. fmt.Println("ItemImages: ")
  79. doc.Find(cItemImages).Each(func(index int, element *goquery.Selection) {
  80. img, _ := element.Attr("src")
  81. imgs = append(imgs, img)
  82. fmt.Println("", img)
  83. })
  84. }
  85. func findEmail(body string, doms string) (emails []string) {
  86. r, _ := regexp.Compile(`[a-zA-Z0-9_+&*-]+(?:\.[a-zA-Z0-9_+&*-]+)*@(?:[a-zA-Z0-9-]+\.)+[a-zA-Z]{2,24}`)
  87. emails = append(emails, r.FindStringSubmatch(body)...)
  88. return
  89. }