def getlyricfromurl(str): data_json = gml.gethtml('http://music.163.com/api/song/lyric?os=pc&' + str + '&lv=-1&kv=-1&tv=-1') data = json.loads(data_json) reg = r'[^0-9\[\]\.\:]' string = re.compile(reg) try: lyriclist = re.findall(string, data['lrc']['lyric']) str_lrc = ','.join(lyriclist) with open('lyric/' + str[3:] + '.txt', 'w+') as file: file.write(str_lrc.replace(',', '')) except: with open('lyric/' + str[3:] + '.txt', 'w+') as file: pass
def getlyricfromurl(sid, c, counts, fails, flist): hm = 'http://music.163.com/api/song/lyric?os=pc&' + sid + '&lv=-1&kv=-1&tv=-1' data_json = gml.gethtml(hm) data = json.loads(data_json) data = json.dumps(data, ensure_ascii=False, encoding='utf8') data = json.loads(data) reg = r'[^0-9\[\]\.\:]' string = re.compile(reg) lyricname = "" ger = ["ac", "rock", "pop", "country"] gtags = [ "(1,0,0,0)\n\n", "(0,1,0,0)\n\n", "(0,0,1,0)\n\n", "(0,0,0,1)\n\n", ] gna = ger[c] gtag = gtags[c] try: st = data["lrc"]["lyric"] # print st lyriclist = re.findall(string, st) # print 1 if 'klyric' in data and "lyric" in data[ 'klyric'] and not data['klyric']["lyric"] is None: lyricname = data['klyric']["lyric"] end = lyricname.index("]") lyricname = lyricname[4:end] # print 2 str_lrc = ','.join(lyriclist) fn = 'lyric/lyric' + gna + '/' + sid[3:] + '.txt' flist.append(fn) # print 3 with open(fn, 'w+') as file: file.write(gtag) file.write(lyricname + '\n') file.write(str_lrc.replace(',', '')) file.close() counts[c] += 1 except: fails[c] += 1 print hm pass print counts[c], fails[c]
temp_1 = item.split(' ') for word in temp_1: word = word.lower() if word not in stopwords: if word not in word_stat: word_stat[word] = 1 else: word_stat[word] += 1 except: pass output = sorted(word_stat.items(), key=lambda item: item[1], reverse=True) with open('fin.dat', 'w+') as point: for thing in output: point.write(str(thing[0])) point.write(' ') point.write(str(thing[1])) point.write('\n') if __name__ == '__main__': print("请输入歌单ID") ID = input('Enter:') html = gml.gethtml("http://music.163.com/playlist?id=" + ID) musiclist = gml.getmusic(html) print(musiclist) for key in musiclist: print(key) getlyricfromurl(key) loadlyricfromfile()