日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程语言 > python >内容正文

python

python-网易云音乐搜索下载脚本

發(fā)布時(shí)間:2023/12/31 python 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python-网易云音乐搜索下载脚本 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

python-網(wǎng)易云音樂搜索下載腳本

  • 前言
  • 開發(fā)環(huán)境及所需的庫
    • 開發(fā)環(huán)境
    • 所需的庫
  • 思想
    • 搜索
    • 下載
    • 保存
  • 實(shí)現(xiàn)
    • 搜索
    • 下載和保存
    • 主函數(shù)和輔助函數(shù)
  • 效果
  • 結(jié)語

前言

之前寫過如何從網(wǎng)易云網(wǎng)頁下載歌曲,但是感覺太麻煩了,于是就想著寫一個(gè)腳本來完成搜索加下載的功能。

開發(fā)環(huán)境及所需的庫

開發(fā)環(huán)境

Windows 10
python 3.7

所需的庫

requests
re
time
selenium
prettytable

思想

搜索

使用爬蟲,訪問https://music.163.com/#/search/m/?s=音樂名&type=1,來的到歌曲列表、歌手以及歌曲ID(下載音樂時(shí)需要)

下載

網(wǎng)易云網(wǎng)頁播放音樂時(shí)會調(diào)用鏈接http://music.163.com/song/media/outer/url?id=音樂ID.mp3,來獲取音樂,所以我們才需要音樂ID

保存

直接將爬取到的mp3保存到文件就行了

實(shí)現(xiàn)

調(diào)用庫

from selenium.webdriver.firefox.options import Options import requests import re from selenium import webdriver from time import sleep from prettytable import PrettyTable

搜索

本來我想用requests庫來實(shí)現(xiàn)歌曲列表的爬取,但是,由于頁面中嵌套著一個(gè)iframe,導(dǎo)致無法得到(也可能是我太菜了)。所以使用selenium來實(shí)現(xiàn)。

我使用的是火狐,先配置selenium,不顯示gui

# 設(shè)置options firefox_options = Options() firefox_options.add_argument("--headless") firefox_options.add_argument("--disable-gpu")

搜索音樂的函數(shù):

# 搜索音樂 def SearchMusic(name):url_search = "https://music.163.com/#/search/m/?s="+name+"&type=1"# 初始化browser = webdriver.Firefox(executable_path="geckodriver.exe", options=firefox_options)# 歌曲搜索browser.get(url=url_search)# 切換iframebrowser.switch_to.frame("g_iframe")sleep(0.5)# 頁面信息page_text = browser.execute_script("return document.documentElement.outerHTML")# 退出browser.quit()# 正則表達(dá)式re1 = '<a.*?id="([0-9]*?)"'re2 = '<b title="(.*?)">.*?<span class="s-fc7">'re3 = 'class="td w1"><div class="text"><a href=".*?">(.*?)</div></div>'id_list = re.findall(re1, page_text, re.M)[::2]song_list = re.findall(re2, page_text, re.M)singer_list = re.findall(re3, page_text, re.M)total_list = list(zip(song_list, singer_list, id_list))# 命令行表格table = PrettyTable(["序號", "音樂名", "歌手", "音樂ID"])for i in range(len(total_list)):# 處理多個(gè)歌手# 處理不完全,可能有BUGif "<a href=" in total_list[i][1]:re4 = '(.*?)</a>'temp = re.findall(re4, total_list[i][1], re.M)re5 = '<a href=".*?">(.*?)</a>'singer = ""for s in temp:t = re.findall(re5, s+"</a>", re.M)if t == []:singer += s + "/"else:singer += t[0] + "/"total_list[i] = (total_list[i][0], singer[:-1], total_list[i][2])else:if total_list[i][1][-1] != ">":temp = total_list[i][1].replace("</a>","")else:temp = total_list[i][1][:-4]total_list[i] = (total_list[i][0], temp, total_list[i][2])# 將數(shù)據(jù)加入到表格table.add_row([str(i+1), total_list[i][0], total_list[i][1], total_list[i][2]])# 輸出表格print(table,"\n")return total_list

火狐的webdrive是geckodriver.exe,可以從網(wǎng)上下載,并放到腳本目錄。
中間的正則表達(dá)式是為了匹配歌曲名、歌手、歌曲ID,同時(shí)對于多個(gè)歌手的情況,不能只用一個(gè)正則表達(dá)式解決,我試了好多次,才可能成功,不一定能保證一定能成功,所以可能存在BUG。
對于列表的輸出,我才用的是prettytable來使得命令行輸出更好看。

下載和保存

下載時(shí)記得設(shè)置header,否則會遇見網(wǎng)絡(luò)訪問頻繁的返回。
下載和保存函數(shù):

# 下載音樂 def GetMusic(music_id):head = {"User-Agent":'Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_8; en-us) AppleWebKit/534.50 (KHTML, like Gecko) Version/5.1 Safari/534.50',"Referer": "https://music.163.com/"}url_music = "http://music.163.com/song/media/outer/url?id="+music_id+".mp3"html = requests.get(url_music, headers=head)with open(music_id+".mp3", "wb") as f:f.write(html.content)

保存到本地時(shí),我是用的是用音樂ID名保存的,所以當(dāng)下載多個(gè)音樂時(shí),要分清楚音樂名。

主函數(shù)和輔助函數(shù)

# 輸出菜單 def menu():print("網(wǎng)易云音樂搜索下載")print("1.音樂搜索")print("2.音樂下載(需要音樂ID)")print("3.退出")print("注: 音樂搜索可以獲得音樂ID\n")# 主函數(shù) def main():while 1:menu()num = input("請輸入序號(1-3):")try:num = int(num)except:print("錯(cuò)誤序號!\n")continueif num not in [1,2,3]:print("錯(cuò)誤序號!\n")if num == 1:name = input("\n輸入歌名(輸入q返回):")if name == "q":continuetotal_list = SearchMusic(name)elif num == 2:music_id = input("\n請輸入歌曲id(輸入q返回):")if music_id == "q":continueprint("下載中")GetMusic(music_id)print("下載完成\n")elif num == 3:break

menu函數(shù)用于輸出菜單。
主函數(shù)用while循環(huán)。

效果

文件:
菜單和搜索:
下載:


結(jié)語

希望大家可以喜歡,代碼文件和geckodriver.exe我會放到GitHub上,希望大家可以點(diǎn)一個(gè)star。

總結(jié)

以上是生活随笔為你收集整理的python-网易云音乐搜索下载脚本的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。