diff --git a/backend/data/stock_data_api.go b/backend/data/stock_data_api.go index 1690893..07af1a6 100644 --- a/backend/data/stock_data_api.go +++ b/backend/data/stock_data_api.go @@ -518,12 +518,26 @@ func SearchStockPriceInfo(stockCode string) *[]string { ) defer cancel() var htmlContent string - err := chromedp.Run(ctx, - chromedp.Navigate(url), - // 等待页面加载完成,可以根据需要调整等待时间 - chromedp.Sleep(3*time.Second), - chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery), - ) + + var tasks chromedp.Tasks + tasks = append(tasks, chromedp.Navigate(url)) + tasks = append(tasks, chromedp.WaitVisible("div.quote-change-box", chromedp.ByQuery)) + tasks = append(tasks, chromedp.ActionFunc(func(ctx context.Context) error { + chromedp.WaitVisible("span.quote-price", chromedp.ByQuery) + price := "" + for { + chromedp.Text("span.quote-price", &price, chromedp.BySearch).Do(ctx) + logger.SugaredLogger.Infof("price:%s", price) + if price != "" { + break + } + } + + return nil + })) + tasks = append(tasks, chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery)) + + err := chromedp.Run(ctx, tasks) if err != nil { logger.SugaredLogger.Error(err.Error()) return &[]string{} @@ -553,7 +567,8 @@ func SearchStockInfo(stock, msgType string) *[]string { err := chromedp.Run(ctx, chromedp.Navigate(url), // 等待页面加载完成,可以根据需要调整等待时间 - chromedp.Sleep(3*time.Second), + //chromedp.Sleep(3*time.Second), + chromedp.WaitVisible("div.search-content,a.search-content", chromedp.ByQuery), chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery), ) if err != nil { diff --git a/backend/data/stock_data_api_test.go b/backend/data/stock_data_api_test.go index fdfe9a0..70cfd01 100644 --- a/backend/data/stock_data_api_test.go +++ b/backend/data/stock_data_api_test.go @@ -44,16 +44,15 @@ func TestGetTelegraph(t *testing.T) { func TestGetTelegraphSearch(t *testing.T) { //url := "https://www.cls.cn/searchPage?keyword=%E9%97%BB%E6%B3%B0%E7%A7%91%E6%8A%80&type=telegram" - messages := SearchStockInfo("闻泰科技", "depth") + messages := SearchStockInfo("闻泰科技", "telegram") for _, message := range *messages { logger.SugaredLogger.Info(message) } //https://www.cls.cn/stock?code=sh600745 } -func TestGetTelegraphSearch2(t *testing.T) { +func TestSearchStockPriceInfo(t *testing.T) { SearchStockPriceInfo("sh600745") - } func TestParseFullSingleStockData(t *testing.T) {