Exemplo n.º 1
0
// FetchRzrqSumData 抓取数据
func FetchDubang(code, ctype string) ([]*Dubang, error) {
	c := "01"
	if strings.ToLower(ctype) == "sz" {
		c = "02"
	}
	formt := "http://soft-f9.eastmoney.com/soft/gp61.php?code=%s%s"

	u := fmt.Sprintf(formt, code, c)

	resp, err := wget.Get(u)
	if err != nil {
		return nil, err
	}

	doc, err := goquery.NewDocumentFromResponse(resp)
	if err != nil {
		return nil, err
	}

	dataset := make([]*Dubang, 0)

	doc.Find("#tablefont tr").Each(func(i int, s *goquery.Selection) {
		if i == 0 {
			s.Find("td").Each(func(k int, td *goquery.Selection) {
				if k == 0 {
					return
				}

				dataset = append(dataset, &Dubang{Date: fmt.Sprint("20", td.Find("p span").Text())})
			})

			return
		}

		s.Find("td").Each(func(k int, td *goquery.Selection) {
			if k == 0 {
				return
			}

			reflect.ValueOf(dataset[k-1]).
				Elem().Field(i).SetFloat(util.ParseMoneyCN(td.Find("p span").Text()))
		})
	})

	return dataset, nil
}
Exemplo n.º 2
0
// FetchRzrqSumData 抓取数据
func FetchCwfx(code, ctype string) ([]*Cwzb, []*Zcfzb, []*Lrb, []*Xjllb, []*FinPercent, error) {
	formt := "http://f10.eastmoney.com/f10_v2/FinanceAnalysis.aspx?code=%s%s"

	resp, err := wget.Get(fmt.Sprintf(formt, ctype, code))
	if err != nil {
		return nil, nil, nil, nil, nil, gos.DoError(err)
	}

	doc, err := goquery.NewDocumentFromResponse(resp)
	if err != nil {
		return nil, nil, nil, nil, nil, gos.DoError(err)
	}

	cwzbList := make([]*Cwzb, 0)
	zcfzbList := make([]*Zcfzb, 0)
	lrbList := make([]*Lrb, 0)
	xjllbList := make([]*Xjllb, 0)
	finPercentList := make([]*FinPercent, 0)
	index := 0

	doc.Find("#F10MainTargetDiv table tr").Each(func(i int, tr *goquery.Selection) {
		if i == 0 {
			tr.Find("th.tips-fieldname-Right").Each(func(k int, th *goquery.Selection) {
				cwzbList = append(cwzbList, &Cwzb{Date: fmt.Sprint("20", th.Text())})
			})
			return
		}

		if _, isOk := tr.Attr("onclick"); isOk {
			tr.Find("td.tips-data-Right").Each(func(k int, td *goquery.Selection) {
				reflect.ValueOf(cwzbList[k]).
					Elem().Field(index + 1).SetFloat(util.ParseMoneyCN(td.Text()))
			})
			index++
		}
	})

	label := ""
	trlist := doc.Find("#BBMX_table tr")
	if trlist.Length() == 60 {
		trlist.Each(func(i int, tr *goquery.Selection) {
			if i == 0 {
				tr.Find("th.tips-fieldname-Right").Each(func(k int, th *goquery.Selection) {
					date := fmt.Sprint("20", th.Text())
					zcfzbList = append(zcfzbList, &Zcfzb{Date: date})
					lrbList = append(lrbList, &Lrb{Date: date})
					xjllbList = append(xjllbList, &Xjllb{Date: date})
				})
				label = "资产负债表"
				index = 0
				return
			}

			if tr.Find("th.tips-colname-Left").Length() > 0 {
				label = tr.Find("th").First().Text()
				index = 0
				return
			}

			tr.Find("td.tips-data-Right").Each(func(k int, td *goquery.Selection) {
				switch label {
				case "资产负债表":
					// fmt.Println(label, util.ParseMoneyCN(td.Text()), index, k)
					reflect.ValueOf(zcfzbList[k]).
						Elem().Field(index + 1).SetFloat(util.ParseMoneyCN(td.Text()))
				case "利润表":
					reflect.ValueOf(lrbList[k]).
						Elem().Field(index + 1).SetFloat(util.ParseMoneyCN(td.Text()))
				case "现金流量表":
					reflect.ValueOf(xjllbList[k]).
						Elem().Field(index + 1).SetFloat(util.ParseMoneyCN(td.Text()))
				}
			})
			index++

		})
	}

	trlist = doc.Find("#PPTable tr")
	if trlist.Length() == 20 {
		trlist.Each(func(i int, tr *goquery.Selection) {
			if i == 0 {
				tr.Find("th.tips-dataC").Each(func(k int, th *goquery.Selection) {
					finPercentList = append(finPercentList, &FinPercent{Date: fmt.Sprint("20", th.Text())})
				})
				return
			}

			if i == 1 {
				return
			}
			index = 0
			tr.Find("td.tips-data-Right").Each(func(k int, td *goquery.Selection) {
				if k%2 == 0 {
					return
				}
				reflect.ValueOf(finPercentList[index]).
					Elem().Field(i - 1).SetFloat(util.ParsePercent(td.Text()))
				index++
			})

		})
	}

	return cwzbList, zcfzbList, lrbList, xjllbList, finPercentList, nil
}