1、获取已下载音乐的歌名路径
F:>dir 歌曲路径 /s /a /b>>"保存路径.txt"
如:我在F盘mp3目录下存储了十多首mp3格式的歌曲
F:\>dir f:\mp3\*.mp3 /s /a /b>>"f:\我的音乐.txt"
2、用notepad++使用正则表达式得到相应的歌曲名及相应的歌手名
例子:歌手 - 歌曲
DJ小鱼儿 - 她会魔法吧
DP龙猪 _ Swei水 _ Rays陈袁 - 风吹一夏
Maksim - Victory
Mohabbatein - Aankhen Khuli
其中需要用到的基础正则表达式为:
汉字字符的正则表达式:[\u4e00-\u9fa5]
歌手信息的正则表达式:[\u4e00-\u9fa5\w_ ()]* -
歌曲信息的正则表达式:- [\w ()(《》)°C?]*
3、将歌曲信息和歌手信息及歌曲路径存储到excel文档中,并使用python进行文档的格式转换,转换为json格式
# 导入 os 模块
import os
# 导入 csv 模块
import csv
# 设置文件夹路径
folder_path = './我的音乐信息/'
# 获取所有文件名
file_list = os.listdir(folder_path)
# 设置文件的表头
head = ['name', 'singer','link_url']
# 设置一个空列表
rows = []
# 遍历所有文件
for file in file_list:
# 通过文件夹拼接文件名的方式,获取文件路径
file_path = folder_path + file
# 以自动关闭的方式打开文件
with open(file_path, 'r', encoding='gbk') as f:
# 以字典的形式获取 csv 文件信息
file_dict = csv.DictReader(f)
# 打印字典的数据
for row in file_dict:
print(row)
# 将读取出来的行数据,循环写入 rows 列表中
rows.append(row)
# 以末尾添加的方式写入文件
with open('./我的音乐信息/new_music.csv', 'w', encoding='gbk') as file:
# 实例化类 DictWriter() 得到 DictWriter 对象
dict_write = csv.DictWriter(file, fieldnames=head)
# 写入文件的表头
dict_write.writeheader()
# 写入文件的多行内容
dict_write.writerows(rows)
在终端输出的文件为单引号,需要使用正则表达式对其进行处理,输出的文件类型如下:
{
"name":"她会魔法吧",
"singer": "DJ小鱼儿",
"link_url":"./source/她会魔法吧.mp3",
},
{
"name":"破茧",
"singer": "张韶涵",
"link_url":"./source/破茧.flac"
},
4、使用python爬取QQ音乐网站周杰伦专辑中的歌曲名称,播放时长、所属专辑
import requests,json
# 设置要请求的网页链接
url = 'https://u.y.qq.com/cgi-bin/musics.fcg?_=1649399026949&sign=zzb2d3f4390onigwaq7ktsr8yv5lydcq73556db8'
# 请求网页
headers = {
'user-agent' : 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Safari/537.36 Core/1.70.3775.400 QQBrowser/10.6.4208.400',
'cookie':'pgv_pvid=3212527756; fqm_pvqid=9e892cdc-fec3-4ddc-ad27-747913e16d8d; fqm_sessionid=35e0d093-04c8-44ef-86db-dd8fa8735b95; pgv_info=ssid=s1027581586; ts_refer=ADTAGmyqq; ts_uid=9974716648; _qpsvr_localtk=0.7807823498916435; Hm_lvt_eaa57ca47dacb4ad4f5a257001a3457c=1649398997; wxrefresh_token=55_Pfp5wmqOSSSyluKOnIkMEJwiARXveRzBr24h_md7yZxn1XFlLbjTdxc-JJtwlK82SfEcjbHKwKFkg86oUEu8AHPvY4HDPOJ_rtUa4vTwUq0; psrf_qqopenid=; psrf_qqrefresh_token=; wxuin=1152921504702680147; euin=oK6kowEAoK4z7eSzowCFoe6P7z**; wxunionid=oqFLxshKwS1zhM95RIbhVUf8r-mA; wxopenid=opCFJw5D0rKHFLTViGkjJMsQkVaE; psrf_qqaccess_token=; qm_keyst=W_X_5dYZfdxLUl1OCh2iBQ7-FFFSOe4OUmEPzXByE1TifxMFlS3h2E-eCMbKfrWlqi_UhUYoYC9uShm8; tmeLoginType=1; wxuin=1152921504702680147; qqmusic_key=W_X_5dYZfdxLUl1OCh2iBQ7-FFFSOe4OUmEPzXByE1TifxMFlS3h2E-eCMbKfrWlqi_UhUYoYC9uShm8; qm_keyst=W_X_5dYZfdxLUl1OCh2iBQ7-FFFSOe4OUmEPzXByE1TifxMFlS3h2E-eCMbKfrWlqi_UhUYoYC9uShm8; psrf_qqunionid=; login_type=2; Hm_lpvt_eaa57ca47dacb4ad4f5a257001a3457c=1649399008; ts_last=y.qq.com/n/ryqq/search'
}
data = '{"comm":{"cv":4747474,"ct":24,"format":"json","inCharset":"utf-8","outCharset":"utf-8","notice":0,"platform":"yqq.json","needNewCode":1,"uin":"1152921504702680147","g_tk_new_20200303":180482182,"g_tk":180482182},"req_1":{"method":"DoSearchForQQMusicDesktop","module":"music.search.SearchCgiService","param":{"remoteplace":"txt.yqq.top","searchid":"71728509400568064","search_type":0,"query":"周杰伦","page_num":1,"num_per_page":10}}}'
data = data.encode('utf-8')
res = requests.post(url,headers=headers,data=data)
# 使用json()方法,将response对象,转为列表/字典
res_con = res.json()['req_1']['data']['body']['song']['list']
for i in res_con:
print('歌名:'+i['name'])
print('所属专辑:'+i['album']['name'])
print('播放时长:'+str(i['interval'])+'秒')