天天看點

Python爬蟲實戰,DecryptLogin子產品,Python模拟登入實作網易雲個人歌單下載下傳器

前言

今天來寫個網易雲音樂個人歌單下載下傳器呗,讓我們愉快地開始吧~

開發工具

** Python版本:**3.6.4

** 相關子產品:**

DecryptLogin子產品;

argparse子產品;

prettytable子產品;

click子產品;

以及一些python自帶的子產品。

環境搭建

安裝Python并添加到環境變量,pip安裝需要的相關子產品即可。

DecryptLogin安裝方式參見(因為經常更新,已經安裝過的小夥伴麻煩記得更新一下,否則可能會在新的案例中報錯)

原理簡介

既然是模拟登入系列,首先自然是先模拟登入網易雲音樂啦,這個利用我們開源的DecrpytLogin庫可以輕松地實作:

'''利用DecryptLogin實作模拟登入'''
@staticmethod
def login(username, password):
  lg = login.Login()
  infos_return, session = lg.music163(username, password)
  return infos_return.get('userid'), session
           

接着就是擷取登入使用者建立/收藏的歌單清單(注意,因為隻是一個小例子,是以僅支援下載下傳登入使用者自己建立/收藏的歌單,當然這代碼應該很容易可以擴充到下載下傳任意歌單T_T),通過抓包分析(其實網上很多地方可以找到别人分析完後公開的網易雲音樂api,需要的可以去知乎或者Github之類的網站上搜尋一下對應關鍵字)我們可以發現登入使用者所有歌單的清單可以通過請求以下API擷取:

https://music.163.com/weapi/user/playlist?csrf_token=
           

其中csrf_token的值在使用者登入後的session的cookies中可以找到,由此我們可以獲得我們需要的歌單相關的資訊,代碼實作如下:

'''獲得所有歌單'''
def getPlayLists(self):
  playlist_url = 'https://music.163.com/weapi/user/playlist?csrf_token='
  playlists = []
  offset = 0
  while True:
    data = {
          "offset": offset,
          "uid": self.userid,
          "limit": 50,
          "csrf_token": self.csrf
        }
    res = self.session.post(playlist_url+self.csrf, headers=self.headers, data=self.cracker.get(data))
    playlists += res.json()['playlist']
    offset += 1
    if not res.json()['more'] == 'false':
      break
  all_playlists = {}
  for item in playlists:
    name = item.get('name')
    track_count = item.get('trackCount')
    play_count = item.get('playCount')
    play_id = item.get('id')
    if item.get('creator').get('userId') == self.userid:
      attr = '我建立的歌單'
    else:
      attr = '我收藏的歌單'
    all_playlists[str(play_id)] = [name, track_count, play_count, attr]
  return all_playlists
           

接着,使用者将選擇想要下載下傳的歌單id,根據歌單id,我們将利用以下api來獲得該歌單的詳細資訊:

https://music.163.com/weapi/v6/playlist/detail?csrf_token=
           

具體而言,代碼實作如下:

def getPlayListSongs(self, playlist_id, num_songs):
  detail_url = 'https://music.163.com/weapi/v6/playlist/detail?csrf_token='
  offset = 0
  song_infos = {}
  while True:
    data = {
          'id': playlist_id,
          'offset': offset,
          'total': True,
          'limit': 1000,
          'n': 1000,
          'csrf_token': self.csrf
        }
    res = self.session.post(detail_url+self.csrf, headers=self.headers, data=self.cracker.get(data))
    tracks = res.json()['playlist']['tracks']
    for track in tracks:
      name = track.get('name')
      songid = track.get('id')
      artists = ','.join([i.get('name') for i in track.get('ar')])
      brs = [track.get('h')] + [track.get('m')] + [track.get('l')]
      song_infos[songid] = [name, artists, brs]
    offset += 1
    if len(list(song_infos.keys())) >= num_songs:
      break
  return song_infos
           

其中傳回的資訊中br(其實就是歌曲比特率)和歌曲id在後續的歌曲下載下傳中是必須要的,其他資訊的提取主要是為了使用者互動的需要。接着,當使用者确定是下載下傳該歌單中的所有歌曲時,程式就開始下載下傳所有歌曲啦。而某首歌曲下載下傳的代碼在之前的音樂下載下傳器裡分享過,copy過來稍微改下大概就是這個這樣了:

'''下載下傳某首歌曲'''
def downloadSong(self, songid, songname, brs, savepath='.'):
  play_url = 'http://music.163.com/weapi/song/enhance/player/url?csrf_token='
  print('正在下載下傳 ——> %s' % songname)
  for br in brs:
    data = {
          'ids': [songid],
          'br': br.get('br'),
          'csrf_token': self.csrf
        }
    res = self.session.post(play_url+self.csrf, headers=self.headers, data=self.cracker.get(data))
    if res.json()['code'] == 200:
      download_url = res.json()['data'][0].get('url', '')
      if download_url:
        break
  with closing(self.session.get(download_url, headers=self.headers, stream=True, verify=False)) as res:
    total_size = int(res.headers['content-length'])
    if res.status_code == 200:
      label = '[FileSize]:%0.2f MB' % (total_size/(1024*1024))
      with click.progressbar(length=total_size, label=label) as progressbar:
        with open(os.path.join(savepath, songname+'.'+download_url.split('.')[-1]), "wb") as f:
          for chunk in res.iter_content(chunk_size=1024):
            if chunk:
              f.write(chunk)
              progressbar.update(1024)
           

文章到這裡就結束了,感謝你的觀看,關注我每天分享Python模拟登入系列,下篇文章分享網抑雲個人聽歌排行榜爬取