一. 介绍二. 步骤三. 代码
一. 介绍能自动下载一个歌手的所有免费歌曲(咪某音乐)
二. 步骤- 首先要安装谷歌游览器自动化插件,选择适合自己游览器版本的插件下载,放到和python脚本同一目录下。选定一个歌手,点进歌手主页,复制主页的URL。填入代码中的url1处在pycharm上运行脚本,扫码登录(这里给了10秒的时间扫码,超时会报错),登陆后就会自动点击下载了,下载完歌手的全部歌曲后,自动结束程序。
import json import re import time from logging import Logger import pyautogui import requests from selenium import webdriver from time import sleep from selenium.webdriver.chrome.options import Options #实现规避检测 from lxml import html etree = html.etree chrome_option = Options() chrome_option.add_argument('--headless') chrome_option.add_argument('--disable-gpu') headers = { 'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.72 Safari/537.36 Edg/89.0.774.45'} url = 'https://music.migu.cn/v3/music/song/6008460PM0U' #在此处填入歌手主页的URL url1 = 'https://music.migu.cn/v3/music/artist/112/song' user_agent = 'Opera/9.80 (Windows NT 6.1; U; zh-cn) Presto/2.9.1 Version/11.50' driver_path = 'chromedriver.exe' opt = webdriver.ChromeOptions() opt.add_argument('--user-agent=%s' % user_agent) opt.add_experimental_option('excludeSwitches', ['enable-automation']) browser = webdriver.Chrome(executable_path=driver_path,options=opt) browser.get(url) logo = browser.find_element_by_xpath('//*[@id="is_songPlay"]').click() # /html/body/div[2]/div[1]/form[2]/div/div[5]/input sleep(10) window = browser.current_window_handle # 获取当前句柄A browser.switch_to.window url0 = 'https://music.migu.cn/v3/music/artist/1001060006/song?page=' url00=url0+str(1) print("即将开始下载") browser.get(url00) j=browser.find_element_by_xpath('/html/body/div[2]/div[2]/div[3]/div/a[3]').text i=1 while i<= int(j): url1 = url0+str(i) print(url1) browser.get(url1) sleep(2) page_text = requests.get(url=url1, headers=headers).text tree = etree.HTML(page_text) li_list = tree.xpath('//*[@id="J_PageSonglist"]/div[2]/div') i+=1 for li in li_list: a_url = 'https://music.migu.cn/v3/music/order/download/' + li.xpath('./@data-cid')[0] browser.get(a_url) window = browser.current_window_handle # 获取当前句柄A browser.switch_to.window print(a_url) sleep(2) pyautogui.click(636, 812, button='left') sleep(2)
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)