parser.go 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. package godo
  2. import (
  3. "kkscrap-go/controllers/scraper/common"
  4. "kkscrap-go/model"
  5. "regexp"
  6. "strings"
  7. util "kkscrap-go/controllers/scraper/util"
  8. "github.com/PuerkitoBio/goquery"
  9. )
  10. func Parse(body string, item *model.ItemInfo) {
  11. item.Language = getLanguage(body)
  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("property")
  16. if v == "og:title" {
  17. item.ItemName = s.AttrOr("content", "")
  18. } else if v == "og:image" {
  19. item.Images = append(item.Images, s.AttrOr("content", ""))
  20. } else if v == "product:price:amount" {
  21. item.SalesPrice = util.GetFloat32(s.AttrOr("content", ""))
  22. } else if v == "product:price:currency" {
  23. item.Currency = s.AttrOr("content", "")
  24. }
  25. })
  26. desc := doc.Find("#frmView > div > div > div.item_detail_tit > div").Text()
  27. if desc != "" {
  28. desc = strings.Split(desc, "\n")[0]
  29. }
  30. item.ShortDesc = desc
  31. item.SalesPrice = getPrice(body)
  32. item.Currency = getCurrency(body)
  33. item.Emails = common.GetEmails(body)
  34. return
  35. }
  36. func getLanguage(body string) string {
  37. reg, _ := regexp.Compile("var gdLocale = '(.*)';")
  38. ss := reg.FindStringSubmatch(body)
  39. return ss[1]
  40. }
  41. func getCurrency(body string) string {
  42. reg, _ := regexp.Compile("var gdCurrencyCode = '(.*)';")
  43. ss := reg.FindStringSubmatch(body)
  44. return ss[1]
  45. }
  46. func getPrice(body string) float32 {
  47. reg, _ := regexp.Compile("'setGoodsPrice'[ ]*:[ ]*'(.*)'")
  48. ss := reg.FindStringSubmatch(body)
  49. return util.GetFloat32(ss[1])
  50. }