mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
refactor(data): 重构股票数据获取方法
- 优化了股票价格信息的获取流程,增加了对关键元素的等待和检查 - 改进了搜索结果页面的处理,使用更准确的选择器进行等待 - 删除了不必要的测试函数,整合了相关的测试用例
This commit is contained in:
parent
1751be729b
commit
40fe30ce2f
@ -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 {
|
||||
|
@ -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) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user