go 获取沪深A股代码
package main import ( "fmt" "io" "io/ioutil" "net/http" "regexp" "strconv" "strings" "github.com/PuerkitoBio/goquery" "golang.org/x/text/encoding/simplifiedchinese" ) func Getlist(url string) []string { var stocks []string req, _ := http.NewRequest("GET", url, nil) // 设置header req.Header.Set("User-Agent", "Mozilla/5.0 (Windows NT 6.1;WOW64) AppleWebKit/537.36 (KHTML,like GeCKO) Chrome/45.0.2454.85 Safari/537.36 115Broswer/6.0.3") req.Header.Set("Referer", "https://movie.douban.com/") req.Header.Set("Connection", "keep-alive") resp, err := (&http.Client{}).Do(req) if err != nil { panic(err) } reader := simplifiedchinese.GB18030.NewDecoder().Reader(resp.Body) doc, err := goquery.NewDocumentFromReader(reader) if err != nil { panic(err) } doc.Find("#quotesearch>ul>li>a"). Each(func(i int, s *goquery.Selection) { //fmt.Printf("%v", s) stock := s.Text() //正则匹配0 3 6 类型的代码 matched, _ := regexp.MatchString(`.+?[036]\d{5}.+?`, stock) if matched { if !strings.Contains(stock, `(688`) { //去掉科创版 stocks = append(stocks, stock) } } }) return stocks } func Index(w http.ResponseWriter, r *http.Request) { url := "http://quote.eastmoney.com/stock_list.html" var stocks []string stocks = Getlist(url) A := strings.Join(stocks[:], "\t") io.WriteString(w, A) if err := ioutil.WriteFile("./stock.txt", []byte(A), 0644); err != nil { fmt.Println(err) } } func main() { http.HandleFunc("/", Index) http.ListenAndServe(":8000", nil) }