获取歌曲id 歌名 作者 封面链接 歌曲链接 热评(评论者 评论者头像 评论)
# 导入包
import requests
import json
import random
#日更[19723756飙升榜 3779629新歌榜] 周四更[3778678热歌榜 2884035原创榜]
song_list_id = 3778678
# 构造url,user-agent请求头参数部分
# get-url
url = 'http://music.163.com/api/playlist/detail'
# get参数
argument = {
'id': str(song_list_id)
}
# 用户代理池
uapools = [
#Edge Trident内核 on win10
"Mozilla/5.0 (Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.140 Safari/537.36 Edge/17.17134",
#Edge chromium内核 on win10
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36 Edg/80.0.361.54",
#Chrome on win10
"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.106 Safari/537.36",
#Safari on mac
"Mozilla/5.0 (Macintosh; U; Intel Mac OS X 10_6_6; en-US) AppleWebKit/533.20.25 (KHTML, like Gecko) Version/5.0.4 Safari/533.20.27",
#FireFox on windows
"Mozilla/5.0 (Windows NT 6.2; WOW64; rv:21.0) Gecko/20100101 Firefox/21.0",
#Opera on mac
"Opera/9.80 (Macintosh; Intel Mac OS X 10.6.8; U; en) Presto/2.9.168 Version/11.52"
]
# User-Agent
ua = uapools[random.randint(0,len(uapools)-1)]
print(ua)
# 向指定的url发送get含有参数的请求
data = requests.post(url,params=argument,headers={'User-Agent' : ua})
# 返回网站对象
res = data.text
# json处理
txt = json.loads(res)
print(txt['result']['tracks'])
#获取歌曲总数
song_all = len(txt['result']['tracks'])
#随机生成歌曲id
rand=random.randint(0,song_all-1)
print(rand)
#歌曲id
song_id = txt['result']['tracks'][rand]['id']
print(song_id)
# 歌曲基本信息
def song_basic(rand):
#歌曲名
song_name = txt['result']['tracks'][rand]['name']
print(song_name)
#歌曲作者
song_author = txt['result']['tracks'][rand]['artists'][0]['name']
print(song_author)
#歌曲封面
song_pic = txt['result']['tracks'][rand]['album']['picUrl']
print(song_pic)
# 歌曲mp3链接
def song_url(song_id):
url = 'https://music.163.com/song/media/outer/url?id='+str(song_id)+'.mp3'
print(url)
# 歌曲热评
def song_hotcomments(song_id):
url = 'http://music.163.com/api/v1/resource/comments/R_SO_4_'+str(song_id)
data = requests.post(url,headers={'User-Agent' : ua})
html = data.text
txt= json.loads(html)
comments_all = len(txt['hotComments'])
for i in range(comments_all):
nickName = txt['hotComments'][i]['user']['nickname']
avatarUrl = txt['hotComments'][i]['user']['avatarUrl']
comment = txt['hotComments'][i]['content']
print(i+1,'\n',nickName,'\n',avatarUrl,'\n',comment)
song_basic(rand)
song_url(song_id)
song_hotcomments(song_id)