溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

怎么用Python爬取酷狗音樂TOP500

發(fā)布時間:2021-09-14 16:32:22 來源:億速云 閱讀:247 作者:chen 欄目:大數(shù)據(jù)

這篇文章主要介紹“怎么用Python爬取酷狗音樂TOP500”,在日常操作中,相信很多人在怎么用Python爬取酷狗音樂TOP500問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么用Python爬取酷狗音樂TOP500”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

怎么用Python爬取酷狗音樂TOP500

上面是網(wǎng)址,

怎么用Python爬取酷狗音樂TOP500

改變數(shù)字就可以實現(xiàn)翻頁,所以這個不能翻頁的問題解決了。然后就是老套路按F12查看找network.

怎么用Python爬取酷狗音樂TOP500

往下翻,發(fā)現(xiàn)這些都有注釋,那就更好辦了。

解析這個數(shù)據(jù),拿出來hash值和filename,歌詞lyric。

也沒什么要說的了,直接貼代碼

import requests
from lxml import etree
import json
import re
import os

class kugou():

   def startkugou(self):
       for i in range(23, 24):
           print(i)
           res = requests.get('https://www.kugou.com/yy/rank/home/%s-8888.html?from=rank' % str(i))
           self.get_song(res)

   def get_song(self, res):
       html = etree.HTML(res.content.decode('utf8'))
       content = html.xpath('//script[10]')
       content2 = content[0].text
       # 解析出json列表,類型是str
       content1 = content2.split('global.features =')[1].split('(function()')[0].strip()[0:-1]
       try:
           # 轉(zhuǎn)換成json數(shù)據(jù)
           content = json.loads(content1)
           for i in range(len(content)):
               hash = content[i]["Hash"]
               file_name = content[i]["FileName"]
               hash_url = "http://www.kugou.com/yy/index.php?r=play/getdata&hash=" + hash
               hash_content = requests.get(hash_url)
               play_url = ''.join(re.findall('"play_url":"(.*?)"', hash_content.text))
               lyrics = ''.join(re.findall('"lyrics":"(.*?)"', hash_content.text))
               real_download_url = play_url.replace("\\", "")
               try:
                   # if os.path.exists('kugou/' + file_name + '.txt'):
                   #     print(file_name + " 歌詞已經(jīng)存在")
                   #     # continue
                   # else:
                   with open('kugou/' + file_name + '.txt', 'w', encoding='utf8')as f:
                       f.write(lyrics.encode('utf8').decode('unicode_escape'))
                   print(file_name + "歌詞已下載完成!")
                   # if os.path.exists('kugou/' + file_name + '.mp3'):
                   #     print(file_name+" 歌曲已經(jīng)存在")
                   #     # continue
                   # else:
                   with open('kugou/' + file_name + ".mp3", "wb")as fp:
                       fp.write(requests.get(real_download_url).content)
                   print(file_name + "歌曲已下載完成!")
               except OSError as e:
                   print("出現(xiàn)異常" + file_name)
                   file_name = self.validateTitle(file_name)
                   # if os.path.exists('kugou/' + file_name + '.txt'):
                   #     print(file_name + " 歌詞已經(jīng)存在")
                   #     # continue
                   # else:
                   with open('kugou/' + file_name + '.txt', 'w', encoding='utf8')as f:
                       f.write(lyrics.encode('utf8').decode('unicode_escape'))
                   print(file_name + "歌詞已下載完成!")
                   # if os.path.exists('kugou/' + file_name + '.mp3'):
                   #     print(file_name + " 歌曲已經(jīng)存在")
                   #     # continue
                   # else:
                   with open('kugou/' + file_name + ".mp3", "wb")as fp:
                       fp.write(requests.get(real_download_url).content)
                   print(file_name + "歌曲已下載完成!")


       except json.decoder.JSONDecodeError as e:
           print(e)
           print(content2)
           content1 = content2.split('global.features =')[1].strip().split('(function() {')[0].strip()
           content1 = content1[0:-1]
           print(content1)

   def validateTitle(self, file_name):
       """ 將 title 名字 規(guī)則化
       :param title: title name 字符串
       :return: 文件命名支持的字符串        """
       rstr = r"[\=\(\)\,\/\\\:\*\?\"\<\>\|\' ']"  # '= ( ) , / \ : * ? " < > |  '   還有空格
       new_title = re.sub(rstr, "_", file_name)  # 替換為下劃線
       return new_title

if __name__ == '__main__':
   kugou().startkugou()

怎么用Python爬取酷狗音樂TOP500

到此,關(guān)于“怎么用Python爬取酷狗音樂TOP500”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

向AI問一下細(xì)節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI