package main import ( "flag" "fmt" "github.com/gocolly/colly" "github.com/tidwall/gjson" "io" "io/ioutil" "log" "net/http" "os" "path/filepath" "regexp" "sync" "time" ) var downloadDestFolder = "D:\\music" var w sync.WaitGroup func main() { url := flag.String("url", "http://yinyue.kuwo.cn/yy/cinfo_73195.htm", "酷我歌单地址") //bangdan:=flag.Int("bang",1,"榜单ID:16-酷我热歌榜;17-酷我新歌榜") flag.Parse() c := colly.NewCollector() c.OnResponse(func(response *colly.Response) { reg := regexp.MustCompile(`jsonm = (.*);`) pat := reg.FindSubmatch(response.Body) jsonStr := string(pat[1]) ParseJson(jsonStr) }) c.Visit(*url) } func ParseJson(content string) { musiclist := gjson.Get(content, "musiclist") //c := colly.NewCollector() if musiclist.Exists() { re := musiclist.Array() for _, v := range re { musicrid := v.Get("musicrid").String() name := v.Get("name").String() //artist := v.Get("artist").String() client := http.Client{Timeout: 900 * time.Second} url := fmt.Sprintf("http://antiserver.kuwo.cn/anti.s?format=mp3&type=convert_url&rid=MUSI_%s&response=url", musicrid) resp, _ := client.Get(url) body, _ := ioutil.ReadAll(resp.Body) resp.Body.Close() w.Add(1) go download(string(body), fmt.Sprintf("%s.mp3", name)) time.Sleep(time.Duration(200) * time.Millisecond) } } w.Wait() } func download(url string, filename string) { defer w.Done() nt := time.Now().Format("2006-01-02 15:04:05") fmt.Printf("[%s]To download %s\n", nt, filename) _ = os.MkdirAll(downloadDestFolder, 0777) fpath := downloadDestFolder + string(filepath.Separator) + filename newFile, err := os.Create(fpath) if err != nil { fmt.Println(err.Error()) } defer newFile.Close() client := http.Client{Timeout: 900 * time.Second} resp, err := client.Get(url) defer resp.Body.Close() _, err = io.Copy(newFile, resp.Body) if err != nil { fmt.Println(err.Error()) } log.Println("Saved:", filename) }