This commit is contained in:
eson 2022-06-23 12:00:19 +08:00
parent 48b62576c4
commit 4c5abd04e4

66
main.go
View File

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