1、查看歌曲
/api.php?s=歌曲名&num=数量
它的属性为
代码如下:
import requestsimport jsondef get_song(SongName):url = "/api.php?s=" + SongName + "&num=10"req=requests.get(url)html=json.loads(req.text)for item in html:print(item["name"],item["picLink"],item["id"],item["singer"],item["url"])if __name__ == "__main__":SongName=input("请输入歌名:")get_song(SongName)
输出结果如下:
2、查看粉丝
请求头:
Referer:/user/fans?id=97526496
获取指定用户的粉丝:
/weapi/user/getfolloweds?csrf_token=
3、动态:
/user/event?id=97526496
/weapi/event/get/97526496?csrf_token=
4、播放
/weapi/song/enhance/player/url
直接播放地址:id可以改为任何存在的歌曲的ID
/outchain/player?type=2&id=516392300&auto=1&height=66&bg=e8e8e8
5、评论(热门评论和全部评论)
520458203这就是歌曲的id
/api/v1/resource/comments/R_SO_4_520458203
爬取歌曲评论的代码:
import requestsimport jsonfrom requests.exceptions import RequestExceptionfrom urllib.parse import urlencodefrom multiprocessing import Pool#多进程池def get_response(offset,limit,SongID):para = { #参数'offset':offset,#页数'limit':limit#总数限制}musicid = str(SongID) # 歌曲IDmusicurl = "/api/v1/resource/comments/R_SO_4_"+musicid+"?"+urlencode(para) #歌曲api地址headers = { #头结构'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8','Accept-Encoding':'gzip, deflate, sdch','Accept-Language':'zh-CN,zh;q=0.8','Cache-Control':'max-age=0','Connection':'keep-alive','Cookie':'vjuids=-13ac1c39b.1620457fd8f.0.074295280a4d9; vjlast=1520491298.1520491298.30; _ntes_nnid=3b6a8927fa622b80507863f45a3ace05,1520491298273; _ntes_nuid=3b6a8927fa622b80507863f45a3ace05; vinfo_n_f_l_n3=054cb7c136982ebc.1.0.1520491298299.0.1520491319539; __utma=94650624.1983697143.1521098920.1521794858.1522041716.3; __utmz=94650624.1521794858.2.2.utmcsr=baidu|utmccn=(organic)|utmcmd=organic; JSESSIONID-WYYY=FYtmJTTpVwmbihVrUad6u76CKxuzXZnfYyPZfK9bi%5CarU936rIdoIiVU50pfQ6JwjGgBvSyZO0%2FR%2BcoboKdPuMztgHCJwzyIgx1ON4v%2BJ2mOvARluNGpRo6lmhA%5CfcfCd3EwdS88sPgxpiiXN%5C6HZZEMQdNRSaHJlcN%5CXY657Faklqdh%3A1522053962445; _iuqxldmzr_=32','Host':'','Upgrade-Insecure-Requests':'1','User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.221 Safari/537.36 SE 2.X MetaSr 1.0'}try:response = requests.post(musicurl,headers=headers)if response.status_code == 200: #如果请求成功return response.content#返回请求内容except RequestException:print("访问出错")def parse_return(html): #解析返回页data = json.loads(html)#将返回的值格式化为jsonif data.get('comments'):comments = data['comments']SongInfo=[]for item in comments:person = []person.append(item['user']['nickname']) #用户的昵称person.append(item['user']['avatarUrl']) #用户的头像URLperson.append(item['content']) #用户的评论内容SongInfo.append(person)return SongInfodef get_song(SongName):url = "/api.php?s=" + SongName + "&num=1" #只搜索一个结果,num表示搜索的结果数req=requests.get(url)html=json.loads(req.text) #加载为JSON格式SongID=""for item in html:print(item["name"],item["picLink"],item["id"],item["singer"],item["url"])SongID=SongID+str(item["id"])return SongID #返回歌曲IDif __name__ == "__main__":SongName=input("请输入歌曲名字:")SongID=get_song(SongName) #得到歌曲IDgethtml = get_response(1,200,SongID) #得到歌曲评论的内容SongInfo=parse_return(gethtml) #返回解析的内容for a,b,c in SongInfo:print(a,b,c)
输出结果如下:
爬取评论的参考链接:带你写爬虫(python)第二篇----抓取网易云音乐下面的评论(API方式) - xiaolei565的专栏 - CSDN博客 /xiaolei565/article/details/79693694