123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960 |
- package godo
- import (
- "kkscrap-go/controllers/scraper/common"
- "kkscrap-go/model"
- "regexp"
- "strings"
- util "kkscrap-go/controllers/scraper/util"
- "github.com/PuerkitoBio/goquery"
- )
- func Parse(body string, item *model.ItemInfo) {
- item.Language = getLanguage(body)
- doc, err := goquery.NewDocumentFromReader(strings.NewReader(body))
- util.CheckError(err)
- doc.Find("meta").Each(func(i int, s *goquery.Selection) {
- v, _ := s.Attr("property")
- if v == "og:title" {
- item.ItemName = s.AttrOr("content", "")
- } else if v == "og:image" {
- item.Images = append(item.Images, s.AttrOr("content", ""))
- } else if v == "product:price:amount" {
- item.SalesPrice = util.GetFloat32(s.AttrOr("content", ""))
- } else if v == "product:price:currency" {
- item.Currency = s.AttrOr("content", "")
- }
- })
- desc := doc.Find("#frmView > div > div > div.item_detail_tit > div").Text()
- if desc != "" {
- desc = strings.Split(desc, "\n")[0]
- }
- item.ShortDesc = desc
- item.SalesPrice = getPrice(body)
- item.Currency = getCurrency(body)
- item.Emails = common.GetEmails(body)
- return
- }
- func getLanguage(body string) string {
- reg, _ := regexp.Compile("var gdLocale = '(.*)';")
- ss := reg.FindStringSubmatch(body)
- return ss[1]
- }
- func getCurrency(body string) string {
- reg, _ := regexp.Compile("var gdCurrencyCode = '(.*)';")
- ss := reg.FindStringSubmatch(body)
- return ss[1]
- }
- func getPrice(body string) float32 {
- reg, _ := regexp.Compile("'setGoodsPrice'[ ]*:[ ]*'(.*)'")
- ss := reg.FindStringSubmatch(body)
- return util.GetFloat32(ss[1])
- }
|