How to use Python to climb KuGou Music TOP500
This article mainly introduces "how to use Python to climb KuGou music TOP500". In daily operation, I believe many people have doubts about how to use Python to climb KuGou music TOP500. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts about "how to use Python to climb KuGou music TOP500". Next, please follow the editor to study!
It's the web address.
You can turn the page by changing the number, so the problem that you can't turn the page is solved. Then it is the old routine to press F12 to find network.
Turn down and find that these are all annotated, which is even easier to do.
Parse this data and take out the hash value and filename, the lyrics lyric.
There's nothing more to say. Just post the code.
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
# parse out the json list with a type of str
Content1 = content2.split ('global.features =') [1] .split ('(function ()') [0] .strip () [0RAPL 1]
Try:
# convert to json data
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 + "lyrics already exist")
# # continue
# else:
With open ('kugou/' + file_name +' .txt', 'wicked, encoding='utf8') as f:
F.write (lyrics.encode ('utf8'). Decode (' unicode_escape'))
Print (file_name + "lyrics downloaded!")
# if os.path.exists ('kugou/' + file_name +' .mp3'):
# print (file_name+ "Song already exists")
# # continue
# else:
With open ('kugou/' + file_name + ".mp3", "wb") as fp:
Fp.write (requests.get (real_download_url) .content)
Print (file_name + "Song has been downloaded!")
Except OSError as e:
Print ("exception" + file_name)
File_name = self.validateTitle (file_name)
# if os.path.exists ('kugou/' + file_name +' .txt'):
# print (file_name + "lyrics already exist")
# # continue
# else:
With open ('kugou/' + file_name +' .txt', 'wicked, encoding='utf8') as f:
F.write (lyrics.encode ('utf8'). Decode (' unicode_escape'))
Print (file_name + "lyrics downloaded!")
# if os.path.exists ('kugou/' + file_name +' .mp3'):
# print (file_name + "Song already exists")
# # continue
# else:
With open ('kugou/' + file_name + ".mp3", "wb") as fp:
Fp.write (requests.get (real_download_url) .content)
Print (file_name + "Song has been downloaded!")
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):
Regularize the name of title
: param title: title name string
: return: the string "supported by file naming
Rstr = r "[\ =\ (\)\,\ /\\:\ *\?\"\ |\'] "#'= (), /\: *?"
< >| 'there is still a space.
New_title = re.sub (rstr, "_", file_name) # replace with an underscore
Return new_title
If _ _ name__ = ='_ _ main__':
Kugou () .startkugou ()
At this point, the study on "how to climb KuGou's music TOP500 with Python" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!