From 6be5c0fa054f6007bbad67b364365c2af9e0eaff Mon Sep 17 00:00:00 2001 From: spark Date: Wed, 12 Feb 2025 22:48:17 +0800 Subject: [PATCH] =?UTF-8?q?refactor(stock=5Fdata):=E4=BC=98=E5=8C=96?= =?UTF-8?q?=E8=82=A1=E7=A5=A8=E4=BF=A1=E6=81=AF=E6=90=9C=E7=B4=A2=E5=8A=9F?= =?UTF-8?q?=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 修改 SearchStockInfo 函数,增加对不同消息类型的处理 - 更新页面等待逻辑,根据消息类型选择不同的选择器 - 调整测试函数,增加时间参数 --- backend/data/stock_data_api.go | 12 ++++++------ backend/data/stock_data_api_test.go | 2 +- 2 files changed, 7 insertions(+), 7 deletions(-) diff --git a/backend/data/stock_data_api.go b/backend/data/stock_data_api.go index 4df85a4..83fc6be 100644 --- a/backend/data/stock_data_api.go +++ b/backend/data/stock_data_api.go @@ -645,7 +645,7 @@ func SearchStockInfo(stock, msgType string, crawlTimeOut int64) *[]string { } }(ctx) var htmlContent string - url := fmt.Sprintf("https://www.cls.cn/searchPage?keyword=%s&type=%s", stock, msgType) + url := fmt.Sprintf("https://www.cls.cn/searchPage?keyword=%s&type=%s", strutil.RemoveNonPrintable(stock), msgType) sel := ".subject-interest-list" if msgType == "depth" { sel = ".subject-interest-list" @@ -656,8 +656,8 @@ func SearchStockInfo(stock, msgType string, crawlTimeOut int64) *[]string { err := chromedp.Run(ctx, chromedp.Navigate(url), // 等待页面加载完成,可以根据需要调整等待时间 - //chromedp.Sleep(3*time.Second), chromedp.WaitVisible(sel, chromedp.ByQuery), + chromedp.Sleep(3*time.Second), chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery), ) if err != nil { @@ -672,10 +672,10 @@ func SearchStockInfo(stock, msgType string, crawlTimeOut int64) *[]string { var messages []string document.Find(".search-telegraph-list,.subject-interest-list").Each(func(i int, selection *goquery.Selection) { text := strutil.RemoveNonPrintable(selection.Text()) - if strings.Contains(text, stock) { - messages = append(messages, text) - logger.SugaredLogger.Infof("搜索到消息-%s: %s", msgType, text) - } + //if strings.Contains(text, strutil.RemoveNonPrintable(stock)) { + messages = append(messages, text) + logger.SugaredLogger.Infof("搜索到消息-%s: %s", msgType, text) + //} }) return &messages } diff --git a/backend/data/stock_data_api_test.go b/backend/data/stock_data_api_test.go index ed86547..d7935ec 100644 --- a/backend/data/stock_data_api_test.go +++ b/backend/data/stock_data_api_test.go @@ -29,7 +29,7 @@ func TestGetFinancialReports(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", 30) + messages := SearchStockInfo("新 希 望", "telegram", 30) for _, message := range *messages { logger.SugaredLogger.Info(message) }