From 4c5abd04e477e670749dd92876282f58126401b9 Mon Sep 17 00:00:00 2001 From: eson Date: Thu, 23 Jun 2022 12:00:19 +0800 Subject: [PATCH] save --- main.go | 66 ++++++++++++++++++++++++++++----------------------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/main.go b/main.go index ddd59e4..d8520fa 100644 --- a/main.go +++ b/main.go @@ -29,23 +29,23 @@ import ( ) type Stock struct { - Date string `json:"日期" bson:"日期"` - CodeStr string `json:"股票代码" bson:"股票代码"` - Name string `json:"名称" bson:"名称"` - ClosingPrice float64 `json:"收盘价" bson:"收盘价"` - MaxPrice float64 `json:"最高价" bson:"最高价"` - MinPrice float64 `json:"最低价" bson:"最低价"` - OpeningPrice float64 `json:"开盘价" bson:"开盘价"` - PreviousClosingPrice float64 `json:"前收盘" bson:"前收盘"` - UpsDowns float64 `json:"涨跌额" bson:"涨跌额"` - UpsDownsRatio float64 `json:"涨跌幅" bson:"涨跌幅"` - TurnoverRate float64 `json:"换手率" bson:"换手率"` - Volume float64 `json:"成交量" bson:"成交量"` - Turnover float64 `json:"成交金额" bson:"成交金额"` - MarketValue float64 `json:"总市值" bson:"总市值"` - CirculatingMarketValue float64 `json:"流通市值" bson:"流通市值"` + Date string `json:"Date" bson:"Date"` // 日期 + CodeStr string `json:"CodeStr" bson:"CodeStr"` // 股票代码 + Name string `json:"Name" bson:"Name"` // 名称 + ClosingPrice float64 `json:"ClosingPrice" bson:"ClosingPrice"` // 收盘价 + MaxPrice float64 `json:"MaxPrice" bson:"MaxPrice"` // 最高价 + MinPrice float64 `json:"MinPrice" bson:"MinPrice"` // 最低价 + OpeningPrice float64 `json:"OpeningPrice" bson:"OpeningPrice"` // 开盘价 + PreviousClosingPrice float64 `json:"PreviousClosingPrice" bson:"PreviousClosingPrice"` // 前收盘 + UpsDowns float64 `json:"UpsDowns" bson:"UpsDowns"` // 涨跌额 + UpsDownsRatio float64 `json:"UpsDownsRatio" bson:"UpsDownsRatio"` // 涨跌幅 + TurnoverRate float64 `json:"TurnoverRate" bson:"TurnoverRate"` // 换手率 + Volume float64 `json:"Volume" bson:"Volume"` // 成交量 + Turnover float64 `json:"Turnover" bson:"Turnover"` // 成交金额 + MarketValue float64 `json:"MarketValue" bson:"MarketValue"` // 总市值 + CirculatingMarketValue float64 `json:"CirculatingMarketValue" bson:"CirculatingMarketValue"` // 流通市值 - Code int `json:"股票数字代码" bson:"股票数字代码"` + Code string `json:"Code" bson:"Code"` // 股票数字代码 } type StockBase struct { @@ -234,6 +234,8 @@ func DownloadDataFromCode(client *mongo.Client, code *StockBase) { // http://quotes.money.163.com/0601988.html + cstock := client.Database("money").Collection("stock") + page := GetDefaultPage() stockurl := fmt.Sprintf("http://quotes.money.163.com/%s.html", code.CODE) log.Println(stockurl) @@ -255,14 +257,19 @@ func DownloadDataFromCode(client *mongo.Client, code *StockBase) { if err != nil { panic(err) } - page.MustNavigate("http://quotes.money.163.com" + urlpath) + page.Navigate("http://quotes.money.163.com" + urlpath) ahref := page.MustElementX("//a[@id='downloadData']") + log.Println("wait downloadData") ahref.WaitEnabled() + time.Sleep(time.Millisecond * 500) ahref.Click(proto.InputMouseButtonLeft) - e := page.MustElementX("//a[@class='blue_btn submit']") - e.WaitEnabled() + e := page.MustElementX("//a[@class='blue_btn submit']") + log.Println("wait blue_btn submit") + + e.WaitEnabled() + time.Sleep(time.Millisecond * 500) w := page.Browser().MustWaitDownload() e.Click(proto.InputMouseButtonLeft) downloaddata := w() @@ -291,7 +298,7 @@ func DownloadDataFromCode(client *mongo.Client, code *StockBase) { re, _ := regexp.Compile(`\d+`) - var stocks []mongo.WriteModel + // var stocks []mongo.WriteModel for _, line := range alls[1:] { var fields []string @@ -301,12 +308,9 @@ func DownloadDataFromCode(client *mongo.Client, code *StockBase) { } - code, err := strconv.Atoi(re.FindString(fields[1])) - if err != nil { - panic(err) - } + code := re.FindString(fields[1]) - s := &Stock{ + s := Stock{ Date: fields[0], CodeStr: fields[1], Name: fields[2], @@ -325,16 +329,12 @@ func DownloadDataFromCode(client *mongo.Client, code *StockBase) { Code: code, } - stocks = append(stocks, &mongo.InsertOneModel{Document: s}) + + cstock.InsertOne(context.TODO(), mongo.InsertOneModel{Document: s}) } - cstock := client.Database("money").Collection("stock") - r, err := cstock.BulkWrite(context.TODO(), stocks) - if err != nil { - - } - log.Println(code.CODE, r) - time.Sleep(time.Second * 2) + log.Println(code.SYMBOL) + time.Sleep(time.Second * 1) } }