The gospel of the Bonaparte, using Python to "gather the wool and get all the music and movies you want"

Posted by ILYAS415 on Tue, 18 Jan 2022 04:19:05 +0100

1, Necessary environment

Recommend a URL analysis website, this website( http://music.zhuolin.wang/ )It's a little awesome. You can analyze VIP videos and VIP music.

If you don't download a large number of music, you can search and download directly here, modify the file name after downloading, and click playlist - click sync on this website to synchronize the song lists of Netease cloud. Then batch download is to download these song lists of Netease cloud.

However, it is unrealistic to download hundreds of songs in a song list manually. Now let's realize the method of downloading hundreds of songs at a time.

Click to synchronize. You need to enter your Netease cloud UID. The way to obtain this UID is as follows: first, open Netease cloud, select a song, right-click to copy the link, and then paste the link anywhere, for example https://music.163.com/song?id=25727803&userid=275613591 The last string of numbers is UID!

2, Program running environment

  • The first step is to install a python 3. This is simple! Paste my version of Python 3 65. Check Add in path during installation
  • The second step is to download FFmpeg, which is used to parse video and audio. As an auxiliary tool for you get, decompress and add environment variables after downloading
  • The third step is to install you get, which is an artifact to download video and audio. If you are interested, you can study it in depth!

That's the environment configuration

3, Reference code

from selenium import webdriver
from selenium.webdriver.common.action_chains import ActionChains
import time, os
# import threding
 
def get_music_name_link():
  main_handle = browser.current_window_handle
  fp = open('E:\\Project_PY\\file\\musiclink.txt','wb')
  fp2 = open('E:\\Project_PY\\file\\musicname.txt','wb')
  try:
    for i in list(range(2,400)):
      browser.switch_to_window(main_handle)
      txt = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div[1]/div/div[%d]' % i).text + '\n'
      fp2.write(bytes(txt,encoding='utf-8'))
      location = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div[1]/div/div[%d]' % i)
      ActionChains(browser).move_to_element(location).perform()
      browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[2]/div[1]/div/div[%d]/span[5]/div/span[2]' % i).click()
      time.sleep(2)
      all_handles = browser.window_handles
      browser.switch_to_window(all_handles[-1]) # lastest
      url_link = browser.current_url + '\n'
      fp.write(bytes(url_link,encoding='utf-8'))
      browser.close()
  except Exception as e:
    print('get_music_name_link meet some problem! {}'.format(e))
    fp.close()
    fp2.close()
 
def download_music(list_name):
  with open('E:\\Project_PY\\file\\musicname.txt','r',encoding='utf-8') as fp1:
      music_name = fp1.readlines()
  len1 = len(music_name)
  fp2 = open('E:\\Project_PY\\file\\musicname_format.txt','w',encoding='utf-8')
  for i in range(3,len1,4):
    music_name_format = music_name[i].strip() + '\n'
    fp2.write(music_name_format)
  fp2.close()
 
  with open('E:\\Project_PY\\file\\musiclink.txt','r',encoding='utf-8') as fp1: 
    with open('E:\\Project_PY\\file\\musicname_format.txt','r',encoding='utf-8') as fp2: 
      for music_link,music_name in zip(fp1.readlines(),fp2.readlines()):
        you_get_link = 'you-get "{}" -o "E:\\Project_PY\\file\\music\\{}" -O "{}"'.format(music_link.strip(),list_name,music_name.strip())
        you_get_link = you_get_link.strip()
        # print(you_get_link)
        os.system(you_get_link)
 
url = 'http://music.zhuolin.wang/'
uid = input('please input your uid:')
options = webdriver.FirefoxOptions()
options.add_argument('--headless')
browser = webdriver.Firefox(firefox_options=options)
browser.implicitly_wait(8)
browser.get(url)
# browser.maximize_window()
browser.set_window_size(1000,100000)
browser.find_element_by_xpath('/html/body/div[3]/div/div[1]/div/span[3]').click()
# scroll = browser.find_element_by_xpath('//*[@id="mCSB_1_dragger_vertical"]')
# ActionChains(browser).drag_and_drop_by_offset(scroll,0,100).perform()
# time.sleep(2)
all_handles = browser.window_handles
browser.switch_to_window(all_handles[-1]) # lastest
time.sleep(1)
browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div[1]/div/span/div[2]/span').click()
all_handles = browser.window_handles
browser.switch_to_window(all_handles[-1]) # lastest
time.sleep(1)
browser.find_element_by_xpath('/html/body/div[6]/div[2]/input').send_keys(uid)
browser.find_element_by_xpath('/html/body/div[6]/div[3]/a[1]').click()
# t1 = threading.Thread(target=get_music_name)
# t2 = threading.Thread(target=get_music_link)
# t3 = threading.Thread(target=download_music)
 
for i in list(range(3,100)):
  try:
    print('downloading song_list{}! please waiting....'.format(i))
    browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div[1]/div/div[%d]/img' % i).click()
    dir_name = browser.find_element_by_xpath('/html/body/div[3]/div/div[2]/div[1]/div[1]/div/div[%d]/p' % i).text
    time.sleep(1)
    get_music_name_link()
    download_music(dir_name)
    browser.find_element_by_xpath('/html/body/div[3]/div/div[1]/div/span[3]').click()
    time.sleep(1)
    all_handles = browser.window_handles
    browser.switch_to_window(all_handles[-1]) # lastest
    time.sleep(5)
  except Exception as e:
    print('get_song_list meet some problem! {}'.format(e))
browser.quit()

4, Core code interpretation

There are three functions:

  • Function 1 get_music_name_link, mainly to get the music name and the music download link
  • Function 2 download_music, get the song list name, then splice the download link and music name, call you get to start downloading to the corresponding directory
  • The third function is main, which mainly uses UID to obtain the song list and batch download the tracks in the song list

Several points needing attention:

  • sleep(1) is used to sleep for one second. If the network is slow, you need to lengthen the sleep time of all sleep
  • If you want to modify the code, be sure to pay attention to switch_to_window to switch windows
  • The 73 line for I in list (range (3100)) is used to select the downloaded song list. The song list is counted from 1

5, Program demonstration

Enter the UID of Netease cloud

Then wait quietly... There will be some music playing sound in this process. If you don't like it, you can turn it on and mute it

When all links are resolved, you get download will be called, and this process will automatically create a folder with the same name as the song list

After downloading

  • Part time communication, industry consultation, big man online professional answers
  • Python development environment installation tutorial
  • Python 400 set self-study video
  • Common vocabulary of software development
  • Python learning Roadmap
  • More than 3000 Python e-books

The above is the whole content of this article. Thank you for watching carefully. For more wonderful Python content, please see my home page and click to get it, Python learning resource sharing, big man online professional answers.

Topics: Python Database AI Deep Learning