import requests
from bs4 import BeautifulSoup
import urllib.request
import re
import os
headers = {
'Referer': 'http://music.163.com/',
'Host': 'music.163.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.75 Safari/537.36',
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
}
url = 'https://music.163.com/discover/playlist/'
base_url = 'https://music.163.com'
save_path = 'D:\\Download\\music\\wangyiyun\\'
def get_song(music,saveurl):
try:
musicUrl = 'http://music.163.com/song/media/outer/url' + music[0][5:] + '.mp3'
name = music[1]
if(not os.path.exists(saveurl+"\\"+music[1] + '%s.mp3')):
urllib.request.urlretrieve(musicUrl, saveurl+"\\"+music[1] + '%s.mp3' % name)
else:
print('%s 歌曲已存在',name)
except Exception as e:
pass
def get_song_url(url):
playurl = base_url + url[1]
s=requests.session()
response = s.get(playurl, headers=headers).text
# 布拉格广场
ex = r'(.*?) '
playurllist = re.findall(ex, response, re.I | re.M)
return playurllist
def get_url_list():
# 获取页面内容`
s = requests.session()
response = s.get(url, headers=headers).text
#
ex = r''
url_list = re.findall(ex, response, re.I | re.M)
# ('朋友请听好 我会陪你笑看风雨', '/playlist?id=4899739055')
return url_list
if __name__ == '__main__':
if not os.path.isdir(save_path): # 目录不存在就创建
os.makedirs(save_path, exist_ok=True)
page = 1
count = 1
urllist = get_url_list()
for url in urllist:
save_url = save_path + url[0]
if not os.path.isdir(save_url): # 目录不存在就创建
os.makedirs(save_url, exist_ok=True)
playlist = get_song_url(url)
for music in playlist:
get_song(music,save_url)
print('本次成功下载 第%d页 第%d个歌曲! %s' % (page,count,music[1]))
count+=1
page += 1