2022年 11月 7日

python爬取指定歌曲的MV

目标网站

音悦Tai:http://www.yinyuetai.com/

爬取结果

在这里插入图片描述
根据指定的歌曲爬取十个相关的mv,存在列表里,十个mv对应十个字典,键为mv的名称,值为mv的数据。
当然也可以根据自己实际爬取需求修改存储的数据结构。
最后爬取下来的数据包括了mv文件的播放链接,可以看到后缀是.flv或.mp4,可以根据这些链接去下载到相应的mv。

数据接口

  1. 根据videoId获取mv数据:
    http://ext.yinyuetai.com/main/get-h-mv-info?json=true&videoId=2275893
  2. 根据关键字获取相关mv数据:
    http://soapi.yinyuetai.com/search/video-search?keyword=那英-默&pageIndex=1&pageSize=24&offset=0&orderType=TOTALVIEWS

上述的两个接口返回的是json数据,大家可自行打开链接观察它们的数据结构。
这两个接口都可以F12打开开发者工具,在音悦Tai发送的网络请求中找到,这里就不多加赘述了。

思路及实现

通过上述的两个数据接口,思路就比较清晰了:
1、通过关键字搜索,得到相关mv的videoId的值,把videoId存储在列表中;
2、循环videoId列表取出每一个videoId,再通过各个videoId得到各个mv数据;
3、存储每一个mv数据。
下面贴出代码:

import requests
import json

# http://soapi.yinyuetai.com/search/video-search?keyword=那英-默&pageIndex=1&pageSize=24&offset=0&orderType=TOTALVIEWS
# http://ext.yinyuetai.com/main/get-h-mv-info?json=true&videoId=2275893

class getMv:
    def getVideoId(self, keyword):
        url = 'http://soapi.yinyuetai.com/search/video-search?keyword=%s&pageIndex=1&pageSize=24&offset=0&orderType=TOTALVIEWS'%keyword
        headers = {
            'Host':'soapi.yinyuetai.com',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
        }
        response = requests.get( url, headers=headers )
        result = json.loads(response.text)
        videoData = result['videos']['data'][:10]   # 只取最热门的前十个MV
        videoId = []
        for vd in videoData:
            videoId.append( vd['id'] )
        return videoId

    def getVideo(self, mvId):
        url = 'http://ext.yinyuetai.com/main/get-h-mv-info?json=true&videoId=%s'%mvId
        headers = {
            'Host':'ext.yinyuetai.com',
            'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.121 Safari/537.36'
        }
        response = requests.get( url, headers=headers )
        result = json.loads(response.text)
        coreVideoInfo = result['videoInfo']['coreVideoInfo']
        #print( coreVideoInfo )
        artistNames = coreVideoInfo['artistNames']
        videoName = coreVideoInfo['videoName']
        videoUrlModels = coreVideoInfo['videoUrlModels']
        video = {}  # 存全部信息
        videoModels = []   # 存MV全部清晰度和地址
        for v in videoUrlModels:
            videoType = {}  # 存各个清晰度与地址
            videoType[v['qualityLevelName']]=v['videoUrl']
            videoModels.append(videoType)
        videoInfo = {}
        videoInfo['video'] = videoModels
        videoInfo['headImage'] = coreVideoInfo['bigHeadImage']


        video[artistNames+' - '+videoName] = videoInfo
        return video

    def main(self, keyword):
        videoId = self.getVideoId( keyword )
        videoInfo = []
        for vid in videoId:
            videoInfo.append( self.getVideo( vid ) )
        return videoInfo

if __name__=='__main__':
    mv = getMv()
#    print(mv.getVideo('2275893'))
#    print( mv.getVideoId( '那英-默' ) )
    mvName = '那英-默'
    print( mv.main( mvName ) )
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61

在实际使用中可根据自身的需求对代码进行修改。

注意:以上内容仅供学习交流使用,请勿用于其它目的。