refactor(data): 重构股票数据获取方法

- 优化了股票价格信息的获取流程,增加了对关键元素的等待和检查
- 改进了搜索结果页面的处理,使用更准确的选择器进行等待
- 删除了不必要的测试函数,整合了相关的测试用例
This commit is contained in:
spark 2025-01-31 13:02:04 +08:00
parent 1751be729b
commit 40fe30ce2f
2 changed files with 24 additions and 10 deletions

View File

@ -518,12 +518,26 @@ func SearchStockPriceInfo(stockCode string) *[]string {
) )
defer cancel() defer cancel()
var htmlContent string var htmlContent string
err := chromedp.Run(ctx,
chromedp.Navigate(url), var tasks chromedp.Tasks
// 等待页面加载完成,可以根据需要调整等待时间 tasks = append(tasks, chromedp.Navigate(url))
chromedp.Sleep(3*time.Second), tasks = append(tasks, chromedp.WaitVisible("div.quote-change-box", chromedp.ByQuery))
chromedp.OuterHTML("html", &htmlContent, 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 { if err != nil {
logger.SugaredLogger.Error(err.Error()) logger.SugaredLogger.Error(err.Error())
return &[]string{} return &[]string{}
@ -553,7 +567,8 @@ func SearchStockInfo(stock, msgType string) *[]string {
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("div.search-content,a.search-content", chromedp.ByQuery),
chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery), chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery),
) )
if err != nil { if err != nil {

View File

@ -44,16 +44,15 @@ func TestGetTelegraph(t *testing.T) {
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("闻泰科技", "depth") messages := SearchStockInfo("闻泰科技", "telegram")
for _, message := range *messages { for _, message := range *messages {
logger.SugaredLogger.Info(message) logger.SugaredLogger.Info(message)
} }
//https://www.cls.cn/stock?code=sh600745 //https://www.cls.cn/stock?code=sh600745
} }
func TestGetTelegraphSearch2(t *testing.T) { func TestSearchStockPriceInfo(t *testing.T) {
SearchStockPriceInfo("sh600745") SearchStockPriceInfo("sh600745")
} }
func TestParseFullSingleStockData(t *testing.T) { func TestParseFullSingleStockData(t *testing.T) {