crawler之网易云热榜歌曲自动下载
这个版本有瑕疵,以后有时间再改进。(瑕疵是终端走代理的情况下有些歌曲会无法下载,其实不算瑕疵,毕竟很少人会把终端全局走代理)
import requests
from lxml import etree
import re
import os
import datetime
def welcome():
'''
This is welcome speech
'''
print("*" * 50)
print(' ' * 15 + '网易云热榜下载小助手')
print(' ' * 5 + 'Author: ctexthuang Date: 2020-05-20 13:14')
print("*" * 50)
def get_NetEase_content(url):
header = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.80 Safari/537.36',
'Referer':'http://93.174.95.27'
}
res_data = requests.get(url,headers = header).text
return res_data
def get_song_content(song_url):
song_header = {
'User-Agent':'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.80 Safari/537.36',
}
song_data = requests.get(song_url,headers = song_header)
if song_data.status_code == 200:
return song_data.content
else:
print(song_url + "下载失败!!")
def check_finder(song_dir):
if not os.path.exists(song_dir):
os.mkdir(song_dir)
def res_song_downloads(song_dir,song_name,song_res):
with open(song_dir+"/"+song_name+".mp3",'wb') as fw:
fw.write(song_res)
fw.close()
print(song_name + "下载成功.\n\r")
def get_Hot_list_id(type):
ids = {
1 : 3778678,
2 : 2884035,
3 : 3779629,
4 : 19723756
}
id = ids.get(type, None)
link = 'http://music.163.com/discover/toplist?id=' + str(id)
return link
def hot_list_downloads(list_type,cycle):
song_dir = datetime.datetime.now().strftime('%Y%m%d')
check_finder(song_dir)
url = get_Hot_list_id(list_type)
res_data = get_NetEase_content(url)
res_etree = etree.HTML(res_data)
lists = res_etree.xpath('//ul[@class="f-hide"]/li/a')
i = 1
for list in lists:
if i <= cycle:
name = list.xpath('./text()')[0]
url = re.findall(r'id=.*',list.xpath('./@href')[0])[0]
song_down_link = "http://music.163.com/song/media/outer/url?"+url
print("第" + str(i) + "首歌曲:" + song_down_link)
print("正在下载...")
song_response = get_song_content(song_down_link)
res_song_downloads(song_dir,name,song_response)
i = i + 1
else:
print('已经全部下载完成!!.\n\r')
break
def run():
list_type = int(input('请选择榜单:1(云音乐热歌榜)/2(网易原创歌曲榜)/3(云音乐新歌榜)/4(云音乐飙升榜)'))
list_type = list_type if list_type else 1
cycle = int(input('请输入下载的数量:(ps:除了云音乐热歌榜最大值只有100,而云音乐热歌榜有200首)'))
cycle = cycle if cycle else 100
if list_type > 4 or list_type < 1:
print('选择榜单错误')
else:
hot_list_downloads(list_type,cycle)
if __name__ == '__main__':
welcome()
run()
跑起来输出如下
# xxx @ xxxdeiMac in ~/Desktop/python/crawler/NetCloud [23:20:08]
$ python netcloud.py
**************************************************
网易云热榜下载小助手
Author: ctexthuang Date: 2020-05-20 13:14
**************************************************
请选择榜单:1(云音乐热歌榜)/2(网易原创歌曲榜)/3(云音乐新歌榜)/4(云音乐飙升榜)1
请输入下载的数量:(ps:除了云音乐热歌榜最大值只有100,而云音乐热歌榜有200首)200
第1首歌曲:http://music.163.com/song/media/outer/url?id=1481164987
正在下载...
会不会(吉他版)下载成功.
第2首歌曲:http://music.163.com/song/media/outer/url?id=1487528112
正在下载...
经济舱 (Live)下载成功.
第3首歌曲:http://music.163.com/song/media/outer/url?id=1443838552
正在下载...
他只是经过下载成功.
第4首歌曲:http://music.163.com/song/media/outer/url?id=1397674264
正在下载...
偏爱下载成功.
第5首歌曲:http://music.163.com/song/media/outer/url?id=1488563891
正在下载...
致明日的舞下载成功.
第6首歌曲:http://music.163.com/song/media/outer/url?id=1426649237
正在下载...
海底下载成功.