前面有介绍Python批量处理网易云音乐缓存文件获取MP3,但是存在一个问题,使用的获取mp3文件名称的模块,很多文件获取不到歌曲名称和歌手信息,后面做了一个完善,通过获取歌曲的网易云音乐id信息在线联网查询歌曲名称,然后重命名文件。
环境准备
1、Selenium 自行安装 2、Google Chrome and webdriver 请移步chrome webdriver下载你电脑chrome版本对应驱动,不知道的百度一下咯。
import os
from mutagen
.mp3
import MP3
from mutagen
.easyid3
import EasyID3
import requests
from selenium
import webdriver
import time
from bs4
import BeautifulSoup
关键代码
1、获取歌曲id
for i
in fList
:
music_id
= i
.split
("-",1)[0]
print(music_id
)
url
= 'https://music.163.com/song?id=%s'%music_id
response
= get_html_src
(url
)
soup
= BeautifulSoup
(response
, "html.parser")
print(soup
.title
.text
.split
("-",1)[0])
decodefile
(os
.path
.join
(CachePath
, i
),os
.path
.join
(SavePath
, (soup
.title
.text
.split
("-",1)[0]+'.mp3')),soup
.title
.text
.split
("-",1)[0], index
)
index
= index
+1
2、隐式使用chrome访问网易在线歌曲,获取歌曲信息
def get_html_src(url
):
option
= webdriver
.ChromeOptions
()
option
.add_argument
('disable-infobars')
driver
= webdriver
.Chrome
(chrome_options
=option
)
driver
.get
(url
)
driver
.switch_to
.frame
("g_iframe")
time
.sleep
(2)
page_src
= driver
.page_source
driver
.close
()
return page_src
源码飞机票