parser.go 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  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(uri string, item *model.ItemInfo) {
  11. body, err := util.Get(uri)
  12. util.CheckError(err)
  13. item.Language = common.GetLanguage(body)
  14. doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
  15. util.CheckError(err)
  16. if len(doc.Find("#sit_title").Nodes) > 0 {
  17. item.ItemName = doc.Find("#sit_title").Nodes[0].FirstChild.Data
  18. }
  19. item.ShortDesc = doc.Find("#sit_desc").Text()
  20. item.SalesPrice = util.GetFloat32(doc.Find("#it_price").AttrOr("value", ""))
  21. item.Currency = getCurrency(body)
  22. item.Images = append(item.Images, doc.Find("#sit_pvi img").AttrOr("src", ""))
  23. return
  24. }
  25. func getLanguage(body string) string {
  26. reg, _ := regexp.Compile("var gdLocale = '(.*)';")
  27. ss := reg.FindStringSubmatch(body)
  28. return ss[1]
  29. }
  30. func getCurrency(body string) string {
  31. return "KRW"
  32. }
  33. func getPrice(body string) string {
  34. //`#it_price`
  35. reg, _ := regexp.Compile("'setGoodsPrice'[ ]*:[ ]*'(.*)'")
  36. ss := reg.FindStringSubmatch(body)
  37. return ss[1]
  38. }