// FetchRzrqSumData 抓取数据 func FetchCwzb(code, ctype string) ([]*Cwzb, error) { ctypeStr := "K" if ctype == "sz" { ctypeStr = "J" } formt := "http://quotes.cnfol.com/new/f10/cwzb/%s%s.html" resp, err := wget.Get(fmt.Sprintf(formt, code, ctypeStr)) if err != nil { return nil, err } doc, err := goquery.NewDocumentFromResponse(resp) if err != nil { return nil, err } doc.Find("table tr").Each(func(i int, s *goquery.Selection) { td := s.Find("td") if td.Length() < 5 { return } td.Each(func(i int, s *goquery.Selection) { if i == 0 { return } fmt.Println(s.Text()) }) }) return nil, nil }
// FetchStockData 抓取数据 func FetchStockData(code string) (*StockIndustry, *StockICB, error) { // http://stockData.stock.hexun.com/600028.shtml formt := "http://stockData.stock.hexun.com/%s.shtml" resp, err := wget.Get(fmt.Sprintf(formt, code)) if err != nil { return nil, nil, gos.DoError(err) } doc, err := goquery.NewDocumentFromResponse(resp) if err != nil { return nil, nil, gos.DoError(err) } tr := doc.Find("#list3 table.box6 tr") stockIndustry := &StockIndustry{} stockICB := &StockICB{} stockIndustry.Name, err = iconv.ConvertString(tr.Eq(7).Find("td").Eq(1).Text(), "gb2312", "utf-8") if err != nil { return nil, nil, gos.DoError(err) } stockICB.Name, err = iconv.ConvertString(tr.Eq(8).Find("td").Eq(1).Text(), "gb2312", "utf-8") if err != nil { return nil, nil, gos.DoError(err) } return stockIndustry, stockICB, nil }
// 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 }
// FetchDzjy 抓取数据 func FetchDzjy(date time.Time) ([]*dzjyVO, error) { formt := "http://data.eastmoney.com/dzjy/%s.html" resp, err := wget.Get(fmt.Sprintf(formt, date.Format("200601"))) if err != nil { return nil, gos.DoError(err) } doc, err := goquery.NewDocumentFromResponse(resp) if err != nil { return nil, gos.DoError(err) } var td *goquery.Selection var dzjy *dzjyVO var dateStr string var stockCode string var row db.DataRow query := db.NewQueryBuilder("stock") datalist := make([]*dzjyVO, 0) buy := "" sell := "" var priceNow float64 var price float64 var amount float64 var total float64 var length int doc.Find("#content div.list").Eq(2).Find("table tr.list_eve").Each(func(i int, tr *goquery.Selection) { td = tr.Find("td") length = td.Length() if length == 10 { dateStr = td.Eq(0).Text() stockCode = td.Eq(1).Text() } else if length == 9 { stockCode = td.Eq(0).Text() } row, _ = query.Where("code=?", stockCode).QueryOne() if row.Empty() { return } switch length { case 10: priceNow = util.ParseMoney(td.Eq(4).Text()) price = util.ParseMoney(td.Eq(5).Text()) amount = util.ParseMoney(td.Eq(6).Text()) total = util.ParseMoney(td.Eq(7).Text()) buy, err = iconv.ConvertString(td.Eq(8).Text(), "gb2312", "utf-8") if err != nil { return } sell, err = iconv.ConvertString(td.Eq(9).Text(), "gb2312", "utf-8") if err != nil { return } case 9: priceNow = util.ParseMoney(td.Eq(3).Text()) price = util.ParseMoney(td.Eq(4).Text()) amount = util.ParseMoney(td.Eq(5).Text()) total = util.ParseMoney(td.Eq(6).Text()) buy, err = iconv.ConvertString(td.Eq(7).Text(), "gb2312", "utf-8") if err != nil { return } sell, err = iconv.ConvertString(td.Eq(8).Text(), "gb2312", "utf-8") if err != nil { return } case 5: price = util.ParseMoney(td.Eq(0).Text()) amount = util.ParseMoney(td.Eq(1).Text()) total = util.ParseMoney(td.Eq(2).Text()) buy, err = iconv.ConvertString(td.Eq(3).Text(), "gb2312", "utf-8") if err != nil { return } sell, err = iconv.ConvertString(td.Eq(4).Text(), "gb2312", "utf-8") if err != nil { return } default: return } dzjy = &dzjyVO{ StockID: row.GetInt64("id"), Date: dateStr, PriceNow: priceNow, Price: price, Amount: amount, Total: total, Buy: buy, Sell: sell, } datalist = append(datalist, dzjy) }) return datalist, nil }
// 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 }