parser.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. package young
  2. import (
  3. "kkscrap-go/controllers/scraper/common"
  4. "kkscrap-go/model"
  5. "strings"
  6. util "kkscrap-go/controllers/scraper/util"
  7. "regexp"
  8. "github.com/PuerkitoBio/goquery"
  9. )
  10. func Parse(body string, item *model.ItemInfo) {
  11. item.Language = common.GetLanguage(body)
  12. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  13. util.CheckError(err)
  14. if len(doc.Find("#sit_title").Nodes) > 0 {
  15. item.ItemName = doc.Find("#sit_title").Nodes[0].FirstChild.Data
  16. }
  17. item.ShortDesc = doc.Find("#sit_desc").Text()
  18. item.SalesPrice = util.GetFloat32(doc.Find("#it_price").AttrOr("value", ""))
  19. item.Currency = getCurrency(body)
  20. item.Images = append(item.Images, doc.Find("#sit_pvi img").AttrOr("src", ""))
  21. return
  22. }
  23. func getLanguage(body string) string {
  24. reg, _ := regexp.Compile("var gdLocale = '(.*)';")
  25. ss := reg.FindStringSubmatch(body)
  26. return ss[1]
  27. }
  28. func getCurrency(body string) string {
  29. return "KRW"
  30. }
  31. func getPrice(body string) string {
  32. //`#it_price`
  33. reg, _ := regexp.Compile("'setGoodsPrice'[ ]*:[ ]*'(.*)'")
  34. ss := reg.FindStringSubmatch(body)
  35. return ss[1]
  36. }