mirror of
https://github.com/ArvinLovegood/go-stock.git
synced 2025-07-19 00:00:09 +08:00
refactor(stock_data):优化股票信息搜索功能
- 修改 SearchStockInfo 函数,增加对不同消息类型的处理 - 更新页面等待逻辑,根据消息类型选择不同的选择器 - 调整测试函数,增加时间参数
This commit is contained in:
parent
d27bcbd334
commit
4fac915778
@ -646,11 +646,18 @@ 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)
|
||||
sel := ".subject-interest-list"
|
||||
if msgType == "depth" {
|
||||
sel = ".subject-interest-list"
|
||||
}
|
||||
if msgType == "telegram" {
|
||||
sel = ".search-telegraph-list"
|
||||
}
|
||||
err := chromedp.Run(ctx,
|
||||
chromedp.Navigate(url),
|
||||
// 等待页面加载完成,可以根据需要调整等待时间
|
||||
//chromedp.Sleep(3*time.Second),
|
||||
chromedp.WaitVisible(".search-content", chromedp.ByQuery),
|
||||
chromedp.WaitVisible(sel, chromedp.ByQuery),
|
||||
chromedp.OuterHTML("html", &htmlContent, chromedp.ByQuery),
|
||||
)
|
||||
if err != nil {
|
||||
@ -663,7 +670,7 @@ func SearchStockInfo(stock, msgType string, crawlTimeOut int64) *[]string {
|
||||
return &[]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())
|
||||
if strings.Contains(text, stock) {
|
||||
messages = append(messages, text)
|
||||
|
@ -20,16 +20,16 @@ import (
|
||||
//-----------------------------------------------------------------------------------
|
||||
|
||||
func TestGetTelegraph(t *testing.T) {
|
||||
GetTelegraphList()
|
||||
GetTelegraphList(30)
|
||||
}
|
||||
|
||||
func TestGetFinancialReports(t *testing.T) {
|
||||
GetFinancialReports("sz000802")
|
||||
GetFinancialReports("sz000802", 30)
|
||||
}
|
||||
|
||||
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("闻泰科技", "telegram")
|
||||
messages := SearchStockInfo("闻泰科技", "depth", 30)
|
||||
for _, message := range *messages {
|
||||
logger.SugaredLogger.Info(message)
|
||||
}
|
||||
@ -41,7 +41,7 @@ func TestSearchStockInfoByCode(t *testing.T) {
|
||||
}
|
||||
|
||||
func TestSearchStockPriceInfo(t *testing.T) {
|
||||
SearchStockPriceInfo("sh600745")
|
||||
SearchStockPriceInfo("sh600745", 30)
|
||||
}
|
||||
|
||||
func TestParseFullSingleStockData(t *testing.T) {
|
||||
|
Loading…
x
Reference in New Issue
Block a user