refactor(stock_data):优化股票信息搜索功能

- 修改 SearchStockInfo 函数,增加对不同消息类型的处理
- 更新页面等待逻辑,根据消息类型选择不同的选择器
- 调整测试函数,增加时间参数
This commit is contained in:
spark 2025-02-12 21:52:27 +08:00
parent d27bcbd334
commit 4fac915778
2 changed files with 13 additions and 6 deletions

View File

@ -646,11 +646,18 @@ func SearchStockInfo(stock, msgType string, crawlTimeOut int64) *[]string {
}(ctx) }(ctx)
var htmlContent string 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", stock, msgType)
sel := ".subject-interest-list"
if msgType == "depth" {
sel = ".subject-interest-list"
}
if msgType == "telegram" {
sel = ".search-telegraph-list"
}
err := chromedp.Run(ctx, err := chromedp.Run(ctx,
chromedp.Navigate(url), chromedp.Navigate(url),
// 等待页面加载完成,可以根据需要调整等待时间 // 等待页面加载完成,可以根据需要调整等待时间
//chromedp.Sleep(3*time.Second), //chromedp.Sleep(3*time.Second),
chromedp.WaitVisible(".search-content", chromedp.ByQuery), chromedp.WaitVisible(sel, chromedp.ByQuery),
chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery), chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery),
) )
if err != nil { if err != nil {
@ -663,7 +670,7 @@ func SearchStockInfo(stock, msgType string, crawlTimeOut int64) *[]string {
return &[]string{} return &[]string{}
} }
var messages []string var messages []string
document.Find(".search-content").Each(func(i int, selection *goquery.Selection) { document.Find(".search-telegraph-list,.subject-interest-list").Each(func(i int, selection *goquery.Selection) {
text := strutil.RemoveNonPrintable(selection.Text()) text := strutil.RemoveNonPrintable(selection.Text())
if strings.Contains(text, stock) { if strings.Contains(text, stock) {
messages = append(messages, text) messages = append(messages, text)

View File

@ -20,16 +20,16 @@ import (
//----------------------------------------------------------------------------------- //-----------------------------------------------------------------------------------
func TestGetTelegraph(t *testing.T) { func TestGetTelegraph(t *testing.T) {
GetTelegraphList() GetTelegraphList(30)
} }
func TestGetFinancialReports(t *testing.T) { func TestGetFinancialReports(t *testing.T) {
GetFinancialReports("sz000802") GetFinancialReports("sz000802", 30)
} }
func TestGetTelegraphSearch(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" //url := "https://www.cls.cn/searchPage?keyword=%E9%97%BB%E6%B3%B0%E7%A7%91%E6%8A%80&type=telegram"
messages := SearchStockInfo("闻泰科技", "telegram") messages := SearchStockInfo("闻泰科技", "depth", 30)
for _, message := range *messages { for _, message := range *messages {
logger.SugaredLogger.Info(message) logger.SugaredLogger.Info(message)
} }
@ -41,7 +41,7 @@ func TestSearchStockInfoByCode(t *testing.T) {
} }
func TestSearchStockPriceInfo(t *testing.T) { func TestSearchStockPriceInfo(t *testing.T) {
SearchStockPriceInfo("sh600745") SearchStockPriceInfo("sh600745", 30)
} }
func TestParseFullSingleStockData(t *testing.T) { func TestParseFullSingleStockData(t *testing.T) {