菜鸡源码,专注精品下载!
当前位置:首页 > 建站教程 > 建站知识

Python脚本实现自动下载歌曲、音乐和歌词

发布时间:2024-01-05  栏目:建站知识   浏览:   分类:python教程 Python脚本

Python自动下载歌曲宝音乐和歌词脚本可以通过以下步骤实现: 1. 使用requests库获取网页源代码; 2. 使用BeautifulSoup库解析网页源代码,提取歌曲链接和歌词链接; 3. 使用requests库下载歌曲文件和歌词文件; 4. 将歌曲文件和歌词文件保存到本地。 以下是一个简单的示例代码: ```python import requests from bs4 import BeautifulSoup # 获取网页源代码 url = 'https://www.example.com' response = requests.get(url) html = response.text # 解析网页源代码,提取歌曲链接和歌词链接 soup = BeautifulSoup(html, 'html.parser') song_link = soup.find('a', {'class': 'song-link'})['href'] lyric_link = soup.find('

发现一个宝藏级音乐下载网站-歌曲宝,里面的各大歌曲都可以免费下载,但是需要一个一个下载,很麻烦,于是写了一端Python脚本,可以自动下载歌曲宝音乐和歌词。

fromseleniumimportwebdriverimportrequestsfrombs4importBeautifulSoupimportosoptions=webdriver.ChromeOptions()options.add_argument('--headless')#给请求指定一个请求头来模拟chrome浏览器headers={'User-Agent':'Mozilla/5.0(WindowsNT10.0;Win64;x64)AppleWebKit/537.36(KHTML,likeGecko)Chrome/54.0.2840.99Safari/537.36','cookie':'__51uvsct__JZKGLNhr7gNTmF1z=1;__51vcke__JZKGLNhr7gNTmF1z=dbcc8135-b908-58b9-ab0f-f09772cc8ef9;__51vuft__JZKGLNhr7gNTmF1z=1673170099915;__vtins__JZKGLNhr7gNTmF1z=%7B%22sid%22%3A%20%2250340dc9-526b-5b41-8642-2fa520c011a5%22%2C%20%22vd%22%3A%2030%2C%20%22stt%22%3A%204104371%2C%20%22dr%22%3A%20616811%2C%20%22expires%22%3A%201673176004282%2C%20%22ct%22%3A%201673174204282%7D'}server='https://www.gequbao.com'#凤凰传奇地址singer='https://www.gequbao.com/s/%E8%B4%B9%E7%8E%89%E6%B8%85'#获取歌曲内容defget_contents(song,song_title,singer_name):#print(song)save_url='G:/python/songs/{}'.format(singer_name)save_lrc_path='G:/python/songs/{}/{}.lrc'.format(singer_name,song_title)res=requests.get(url=song,headers=headers)res.encoding='utf-8'html=res.textsoup=BeautifulSoup(html,'html.parser')#获取歌曲的下载链接driver=webdriver.Chrome(options=options)driver.get(song)song_elem=driver.find_element_by_id("btn-download-mp3")lrc_elem=driver.find_element_by_id("btn-download-lrc")download_url=song_elem.get_attribute('href')lrc_url=lrc_elem.get_attribute('href')#读取MP3资源req=requests.get(download_url,stream=True)#文件夹不存在,则创建文件夹folder=os.path.exists(save_url)ifnotfolder:os.makedirs(save_url)#文件存储地址full_title=song_title+'.mp3'file_path=os.path.join(save_url,full_title)print('开始写入歌曲:',file_path)#打开本地文件夹路径file_path,以二进制流方式写入,保存到本地withopen(file_path,'wb')asfd:forchunkinreq.iter_content():fd.write(chunk)print(song_title+'成功下载!')#下载歌词print('开始写入歌词:',save_lrc_path)r=requests.get(lrc_url).contentwithopen(save_lrc_path,'wb')asfd:fd.write(r)fd.close()print(song_title+'歌词成功下载!')#主方法defmain():res=requests.get(singer,headers=headers)res.encoding='utf-8'html=res.text#使用自带的html.parser解析soup=BeautifulSoup(html,'html.parser')#获取歌曲的列表songs=soup.find('div',class_='card-text').find_all(class_='text-primary')singer_name=soup.find('input',id='s-input-line')['value']print('查询到歌曲数:%d'%len(songs))foreachinsongs:try:song=server+each.get('href')song_title=each.get_text().strip()get_contents(song,song_title,singer_name)exceptExceptionase:print(e)if__name__=='__main__':main()

运行之后需要输入要下载的歌手搜索结果页

下载速度有点慢,而且访问太频繁系统会返回443,但是满足需求了。

评论
建站知识
建站知识
使用技巧
调试安装
运营推广