// 更新上市公司列表 func (m America) Companies() ([]Company, error) { urls := [...]string{ "http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NASDAQ&render=download", "http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=NYSE&render=download", "http://www.nasdaq.com/screening/companies-by-industry.aspx?exchange=AMEX&render=download", } list := make([]Company, 0) for _, url := range urls { // 尝试从网络获取实时上市公司列表 csv, err := io.DownloadStringRetry(url, retryTimes, retryIntervalSeconds) if err != nil { return nil, err } // 解析CSV companies, err := m.parseCSV(csv) if err != nil { return nil, err } list = append(list, companies...) } // 按Code排序 sort.Sort(CompanyList(list)) return list, nil }
// 从雅虎财经获取上市公司分时数据 func DownloadCompanyDaily(marketName, companyCode, queryCode string, day time.Time) error { // 检查数据库是否解析过,解析过的不再重复解析 found := raw60Exists(marketName, companyCode, day) if found { return nil } // 如果不存在就抓取 start := time.Date(day.Year(), day.Month(), day.Day(), 0, 0, 0, 0, day.Location()) end := start.Add(time.Hour * 24) pattern := "https://finance-yql.media.yahoo.com/v7/finance/chart/%s?period2=%d&period1=%d&interval=1m&indicators=quote&includeTimestamps=true&includePrePost=true&events=div%7Csplit%7Cearn&corsDomain=finance.yahoo.com" url := fmt.Sprintf(pattern, queryCode, end.Unix(), start.Unix()) // 查询Yahoo财经接口,返回股票分时数据 content, err := io.DownloadStringRetry(url, retryTimes, retryIntervalSeconds) if err != nil { return err } raw := Raw60{ Market: marketName, Code: companyCode, Date: day.UTC(), Json: content, Status: 0, Message: ""} // 保存(加入保存队列) saveQueue <- raw return nil }
// 更新上市公司列表 func (m HongKong) Companies() ([]Company, error) { urls := [...]string{ "https://www.hkex.com.hk/chi/market/sec_tradinfo/stockcode/eisdeqty_c.htm", "https://www.hkex.com.hk/chi/market/sec_tradinfo/stockcode/eisdgems_c.htm", } companies := make([]Company, 0) for _, url := range urls { // 尝试从网络获取实时上市公司列表 html, err := io.DownloadStringRetry(url, retryTimes, retryIntervalSeconds) if err != nil { return nil, err } // 解析json hk, err := m.parseHtml(html) if err != nil { return nil, err } companies = append(companies, hk...) } // 按Code排序 sort.Sort(CompanyList(companies)) return companies, nil }
// 深圳证券交易所上市公司 func (m China) shenzhenCompanies() ([]Company, error) { urls := [...]string{ "http://www.szse.cn/szseWeb/FrontController.szse?ACTIONID=8&CATALOGID=1110&tab1PAGENUM=1&ENCODE=1&TABKEY=tab1", } list := make([]Company, 0) for _, url := range urls { // 尝试从网络获取实时上市公司列表 html, err := io.DownloadStringRetry(url, retryTimes, retryIntervalSeconds) if err != nil { return nil, err } // 深圳证券交易所的查询结果是GBK编码的,需要转成UTF8 html, err, _, _ = gogb2312.ConvertGB2312String(html) if err != nil { return nil, err } // 解析Html companies, err := m.parseShenzhenHtml(html) if err != nil { return nil, err } list = append(list, companies...) } return list, nil }