Python抓取网易云 歌词,热评,热评用户的头像

in:key word

out:歌词,热评,热评用户的头像下载




import requests 
import json
import os
from urllib.request import urlretrieve
 
def GetConment(kw):
    url = 'http://120.79.36.48/search?keywords=%20'+kw
    resp = requests.get(url)
    resp.encoding = 'utf8'
    hjson = resp.json() 
    # if  not 'songs' in hjson:
    #     return 'No Lyric'
    songs = [] 
    for x in range(0,len(hjson['result']['songs'])):
        # print(hjson['result']['songs'][x]['id'])
        songs.append(hjson['result']['songs'][x]['id'])
    # print(songs)
    if len(songs)==0:
        return 'No Songs' 
    cnt = 0 
    for s in songs: 
        commenturl = 'http://120.79.36.48/comment/music?id='+str(s)+'&limit=1'
        # print(liricsurl)
        r = requests.get(commenturl)
        r.encoding = 'utf8'
        json = r.json() 
        comment = json['hotComments'] 
        for i in range(0,len(comment)-1):
            print('『'+str(cnt)+'』'+comment[i]['content'])
            cnt = cnt +1 
            print(comment[i]['user']['avatarUrl'])
            # cont = cont + comment[i]['content']



def DawnloadHeader(kw):
    url = 'http://120.79.36.48/search?keywords=%20'+kw
    resp = requests.get(url)
    resp.encoding = 'utf8'
    hjson = resp.json() 
    # if  not 'songs' in hjson:
    #     return 'No Lyric'
    songs = [] 
    for x in range(0,len(hjson['result']['songs'])):
        # print(hjson['result']['songs'][x]['id'])
        songs.append(hjson['result']['songs'][x]['id'])
    # print(songs)
    if len(songs)==0:
        return 'No Songs'
    
    cnt = 0
    dirname = kw+'Header'
    if not os.path.exists(dirname):
        os.makedirs(dirname)
    for s in songs: 
        commenturl = 'http://120.79.36.48/comment/music?id='+str(s)+'&limit=1'
        # print(liricsurl)
        r = requests.get(commenturl)
        r.encoding = 'utf8'
        json = r.json() 
        comment = json['hotComments'] 
        for i in range(0,len(comment)-1):
            print('正在下载:'+comment[i]['user']['avatarUrl'])
            filename=os.path.join(dirname,str(cnt)+comment[i]['user']['nickname']+'.jpg')
            with open(filename,'w') as f:
                urlretrieve(comment[i]['user']['avatarUrl'],filename)
            cnt = cnt +1

    print('已下载完!')

def Getlirics(kw,cnt):
    url = 'http://120.79.36.48/search?keywords=%20'+kw
    resp = requests.get(url)
    resp.encoding = 'utf8'
    hjson = resp.json() 
    # if  not 'songs' in hjson:
    #     return 'No Lyric'
    songs = [] 
    for x in range(0,cnt):
        # print(hjson['result']['songs'][x]['id'])
        songs.append(hjson['result']['songs'][x]['id'])
    # print(songs)
    if len(songs)==0:
        return 'No Lyric'
    
    lir = ''
    for s in songs: 
        liricsurl = 'http://120.79.36.48/lyric?id='+str(s) 
        # print(liricsurl)
        r = requests.get(liricsurl)
        r.encoding = 'utf8'
        json = r.json() 
        lyric = json['lrc']['lyric']  
        lyricUser = 'No User' 
        if 'lyricUser' in json:
            lyricUser = json['lyricUser']['nickname'] 
        # print('['+lyricUser+']\n'+lyric)
        lir = lir + '『'+lyricUser+'』\n'+lyric+'\n'
    if lir == '':
        return ''
    return lir
    
DawnloadHeader('你的名字')

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值