From 40fe30ce2f63e41b43bebead4704cfcd8a318ffe Mon Sep 17 00:00:00 2001 From: spark Date: Fri, 31 Jan 2025 13:02:04 +0800 Subject: [PATCH] =?UTF-8?q?refactor(data):=20=E9=87=8D=E6=9E=84=E8=82=A1?= =?UTF-8?q?=E7=A5=A8=E6=95=B0=E6=8D=AE=E8=8E=B7=E5=8F=96=E6=96=B9=E6=B3=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 优化了股票价格信息的获取流程,增加了对关键元素的等待和检查 - 改进了搜索结果页面的处理,使用更准确的选择器进行等待 - 删除了不必要的测试函数,整合了相关的测试用例 --- backend/data/stock_data_api.go | 29 ++++++++++++++++++++++------- backend/data/stock_data_api_test.go | 5 ++--- 2 files changed, 24 insertions(+), 10 deletions(-) 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) {