parser.go 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. package dabory
  2. import (
  3. "kkscrap-go/model"
  4. "log"
  5. "regexp"
  6. "strconv"
  7. "strings"
  8. util "kkscrap-go/controllers/scraper/util"
  9. "github.com/PuerkitoBio/goquery"
  10. )
  11. func Parse(body string, item *model.ItemInfo) {
  12. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  13. util.CheckError(err)
  14. doc.Find("meta").Each(func(i int, s *goquery.Selection) {
  15. v, _ := s.Attr("name")
  16. if v == "SolutionType" {
  17. item.SolutionName = model.SolutionType(s.AttrOr("content", ""))
  18. } else if v == "email" {
  19. item.Emails = append(item.Emails, s.AttrOr("content", ""))
  20. }
  21. })
  22. item.ItemCategory = doc.Find(".dbr-item-category").Text()
  23. item.ItemName = doc.Find(".dbr-item-name").Text()
  24. item.BrandName = doc.Find(".dbr-brand-name").Text()
  25. str := doc.Find(".dbr-sales-price").Text()
  26. str = strings.ReplaceAll(str, ",", "")
  27. str = strings.ReplaceAll(str, "원", "")
  28. str = strings.TrimSpace(str)
  29. p, err := strconv.ParseInt(str, 10, 64)
  30. if err != nil {
  31. p = 0
  32. log.Println(str, err)
  33. }
  34. item.SalesPrice = float32(p)
  35. item.OriginDesc, err = doc.Find(".dbr-origin-desc").Html()
  36. util.CheckError(err)
  37. item.Images = append(item.Images, doc.Find(".dbr-image-preview").AttrOr("src", ""))
  38. re := regexp.MustCompile(`-\d+x\d+\.`)
  39. doc.Find(".dbr-image-thumb").Each(func(i int, s *goquery.Selection) {
  40. img := s.AttrOr("src", "")
  41. img = re.ReplaceAllString(img, ".")
  42. item.Images = append(item.Images, img)
  43. })
  44. opt := model.Option{}
  45. doc.Find(".dbr-option1 > option").Each(func(i int, selection *goquery.Selection) {
  46. opt.Choices = append(opt.Choices, model.Choice{
  47. Name: selection.Text(),
  48. })
  49. })
  50. item.Options = append(item.Options, opt)
  51. opt = model.Option{}
  52. doc.Find(".dbr-option2 > option").Each(func(i int, selection *goquery.Selection) {
  53. opt.Choices = append(opt.Choices, model.Choice{
  54. Name: selection.Text(),
  55. })
  56. })
  57. item.Options = append(item.Options, opt)
  58. return
  59. }
  60. func getCurrency(body string) string {
  61. reg, _ := regexp.Compile("var gdCurrencyCode = '(.*)';")
  62. ss := reg.FindStringSubmatch(body)
  63. return ss[1]
  64. }
  65. func getPrice(body string) float32 {
  66. reg, _ := regexp.Compile("'setGoodsPrice'[ ]*:[ ]*'(.*)'")
  67. ss := reg.FindStringSubmatch(body)
  68. return util.GetFloat32(ss[1])
  69. }