Skip to content

Commit

Permalink
refactor(data): 重构股票数据获取方法
Browse files Browse the repository at this point in the history
- 优化了股票价格信息的获取流程,增加了对关键元素的等待和检查
- 改进了搜索结果页面的处理,使用更准确的选择器进行等待
- 删除了不必要的测试函数,整合了相关的测试用例
  • Loading branch information
spark committed Jan 31, 2025
1 parent 1751be7 commit 40fe30c
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 10 deletions.
29 changes: 22 additions & 7 deletions backend/data/stock_data_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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{}
Expand Down Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions backend/data/stock_data_api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 40fe30c

Please sign in to comment.