Esempio n. 1
0
func FetchDistrictFoodPrice(district string) ([]*DistrictFoodPrice, error) {
	districtFoodPrices := make([]*DistrictFoodPrice, 0)
	url, urlErr := GetDistrictFoodPriceUrl(district)
	if urlErr != nil {
		return districtFoodPrices, urlErr
	}
	url = fmt.Sprintf(url+"?_=%d", time.Now().UTC().UnixNano())
	var doc *goquery.Document
	var e error
	if doc, e = goquery.NewDocument(url); e != nil {
		return districtFoodPrices, e
	}

	acquisitionTimeSele := doc.Find("#maincontent li").FilterFunction(func(i int, s *goquery.Selection) bool {
		content := s.Text()
		return strings.Contains(content, "采价时间")
	})
	time, convertErr := convertPageTimeToTime(acquisitionTimeSele.Text())
	if convertErr != nil {
		return districtFoodPrices, errors.New("parse time error!")
	}

	titles := make([]string, 0)
	doc.Find("#WebSitePrice tr.title td").Each(func(i int, s *goquery.Selection) {
		titles = append(titles, strings.TrimSpace(s.Text()))
	})

	doc.Find("#WebSitePrice tr").FilterFunction(func(i int, s *goquery.Selection) bool {
		return !s.HasClass("title")
	}).Each(func(i int, s *goquery.Selection) {
		districtFoodPrice := &DistrictFoodPrice{}
		districtFoodPrice.District = district
		districtFoodPrice.Time = time
		s.Find("td").Each(func(tdI int, tdSele *goquery.Selection) {
			if tdI == 0 {
				districtFoodPrice.Food = strings.TrimSpace(tdSele.Text())
				return
			}
			priceSite := &PriceSite{}
			priceSite.Price, _ = strconv.ParseFloat(tdSele.Text(), 64)
			priceSite.Site = titles[tdI]
			districtFoodPrice.PricesSites = append(districtFoodPrice.PricesSites, *priceSite)
		})
		districtFoodPrices = append(districtFoodPrices, districtFoodPrice)
	})
	return districtFoodPrices, nil
}
Esempio n. 2
0
func FetchCityFoodPrice(city string) ([]*CityFoodPrice, error) {
	cityFoodPrices := make([]*CityFoodPrice, 0)
	url, urlErr := GetCityFoodPriceUrl(city)
	if urlErr != nil {
		return cityFoodPrices, urlErr
	}
	// do not use server cache
	url = fmt.Sprintf(url+"?_=%d", time.Now().UTC().UnixNano())
	var doc *goquery.Document
	var e error
	if doc, e = goquery.NewDocument(url); e != nil {
		return cityFoodPrices, e
	}

	acquisitionTimeSele := doc.Find("#maincontent li").FilterFunction(func(i int, s *goquery.Selection) bool {
		content := s.Text()
		return strings.Contains(content, "采价时间")
	})
	time, convertErr := convertPageTimeToTime(acquisitionTimeSele.Text())
	if convertErr != nil {
		return cityFoodPrices, errors.New("parse time error!")
	}

	doc.Find("#WebSitePrice tr").FilterFunction(func(i int, s *goquery.Selection) bool {
		return !s.HasClass("title")
	}).Each(func(i int, s *goquery.Selection) {
		fields := s.Find("td")
		cityFoodPrice := &CityFoodPrice{}
		cityFoodPrice.City = city
		cityFoodPrice.Time = time
		cityFoodPrice.Food = strings.TrimSpace(fields.Eq(0).Text())
		cityFoodPrice.Unit = strings.TrimSpace(fields.Eq(2).Text())
		cityFoodPrice.AvgPrice, _ = strconv.ParseFloat(fields.Eq(3).Text(), 64)
		cityFoodPrice.MaxPrice, _ = strconv.ParseFloat(fields.Eq(4).Text(), 64)
		cityFoodPrice.MaxSite = strings.TrimSpace(fields.Eq(5).Text())
		cityFoodPrice.MinPrice, _ = strconv.ParseFloat(fields.Eq(6).Text(), 64)
		cityFoodPrice.MinSite = strings.TrimSpace(fields.Eq(7).Text())
		cityFoodPrices = append(cityFoodPrices, cityFoodPrice)
	})
	return cityFoodPrices, nil
}