目录

准备工作

1.网站抓包

2.源代码及效果


Python的Seleinum库来爬取音乐,搜索音乐程序。seleinum库,一个用于动态数据获取的库,可以操控电脑浏览器,就像人一样操控。因此到达获取动态数据的效果。(总之好玩就是了)


准备工作

1.安装seleinum和requests及BeautifulSoup相应库

pip install seleinum
pip install requests
pip install bs4

2.安装浏览器驱动,此次使用的是google浏览器还要安装浏览器驱动才可以使用

请先安装好谷歌浏览器,然后查看谷歌浏览器版本,再确定下载驱动

python提取音频文件中的常见特征 python提取伴奏_xml

python提取音频文件中的常见特征 python提取伴奏_selenium_02

图上所示就是版本号查看方式咯

然后把驱动下载好后放在电脑上Python的Scripts文件夹中

python提取音频文件中的常见特征 python提取伴奏_xml_03

 

python提取音频文件中的常见特征 python提取伴奏_xml_04

这样, 准备工作就完成了!


1.网站抓包

我们先进行音乐下载部分进行编写 

搜索一个音乐打开工具

很快就发现了音乐地址,但是,那是动态内容,那我们就可以用seleinum获取源码

python提取音频文件中的常见特征 python提取伴奏_爬虫_05

 

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests
from bs4 import BeautifulSoup as be
import re
import time

"""
初始化参数
"""
EDGE=webdriver.ChromeOptions()
EDGE.add_argument('--headless')

"""
获取网页中的音乐地址
"""
def get_video(url):
    global EDGE
    br=webdriver.Chrome(options=EDGE)
    br.get(url)
    audio=be(br.page_source, "lxml").audio#find("div", class_="music", id="myAudio")
    br.quit()
    return audio.attrs['src']

 这样就可以获取网页中的音乐地址了,接下来就是获取酷狗音乐的排行榜。

这个网页就可以用requests直接获取到

python提取音频文件中的常见特征 python提取伴奏_xml_06

  这个就简单多了,链接就在列表中,直接获取就行了

"""
获取歌曲排名
"""
def get_phb():
    phb={}
    headers={
        "User-Agent":"这里写你自己的"
    }
    r=requests.get(url="https://www.kugou.com/yy/rank/home/1-8888.html?from=rank", headers=headers)
    div=be(r.text,"lxml").find("div", class_="pc_temp_songlist").ul.find_all("li")
    for i in div:
        phb[i.a.attrs['title']]=i.a.attrs['href']
    return phb

接下来就是最难的搜索模块了!回到主页搜索一个歌曲,查看网页成这样

python提取音频文件中的常见特征 python提取伴奏_爬虫_07

再查看源码,既然指向了另一个网页,我们打开看

python提取音频文件中的常见特征 python提取伴奏_python提取音频文件中的常见特征_08

python提取音频文件中的常见特征 python提取伴奏_selenium_09

  指向了一个音乐播放地址也就是上面写的代码页面,这就可以这样写了!

"""
搜索功能
"""
def search(SongName):
    global EDGE
    headers={
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62"
    }
    
    '''
    selenium爬取网页中的源代码
    '''
    url=f"https://www.kugou.com/yy/html/search.html#searchType=song&searchKeyWord={SongName}"
    br=webdriver.Chrome(options=EDGE)
    br.get(url)
    li=be(br.page_source, "lxml").find("div", class_="song_list").find("ul", class_="list_content").find_all("li")
    br.quit()   #   退出

    '''
    处理数据获取源代码中的网络地址
    '''
    #   音乐名字
    songname=li[0].find("a", class_="song_name").attrs['title']
    print(f"音乐名字是:{songname}")
    #   音乐文件地址
    songurl=li[0].find("div", class_="width_s_li").find("a", class_="album_name").attrs['href']
    r=requests.get(url=songurl, headers=headers)
    URL=be(r.text,"lxml").find("div", id="songs", class_="list1").ul.li.a.attrs['href']
    print("URL:",get_video(URL))

这样所有功能就做出来了!


2.源代码及效果

 get_video.py

from selenium import webdriver
from selenium.webdriver.chrome.options import Options
import requests
from bs4 import BeautifulSoup as be
import re
import time

"""
初始化参数
"""
EDGE=webdriver.ChromeOptions()
EDGE.add_argument('--headless')

"""
获取网页中的音乐地址
"""
def get_video(url):
    global EDGE
    br=webdriver.Chrome(options=EDGE)
    br.get(url)
    audio=be(br.page_source, "lxml").audio#find("div", class_="music", id="myAudio")
    br.quit()
    return audio.attrs['src']
"""
获取酷狗歌曲排名
"""
def get_phb():
    phb={}
    headers={
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62"
    }
    r=requests.get(url="https://www.kugou.com/yy/rank/home/1-8888.html?from=rank", headers=headers)
    div=be(r.text,"lxml").find("div", class_="pc_temp_songlist").ul.find_all("li")
    for i in div:
        phb[i.a.attrs['title']]=i.a.attrs['href']
    return phb
"""
保存酷狗音乐排名音乐
"""
def pre_phb_video():
    with open("index.txt", "w", encoding="utf-8") as f:
        for name,url in get_phb().items():
            f.write(f"Name:{name}Audio:{get_video(url)}\n")

"""
搜索功能
"""
def search(SongName):
    global EDGE
    headers={
        "User-Agent":"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/96.0.4664.110 Safari/537.36 Edg/96.0.1054.62"
    }
    
    '''
    selenium爬取网页中的源代码
    '''
    url=f"https://www.kugou.com/yy/html/search.html#searchType=song&searchKeyWord={SongName}"
    br=webdriver.Chrome(options=EDGE)
    br.get(url)
    li=be(br.page_source, "lxml").find("div", class_="song_list").find("ul", class_="list_content").find_all("li")
    br.quit()   #   退出

    '''
    处理数据获取源代码中的网络地址
    '''
    #   音乐名字
    songname=li[0].find("a", class_="song_name").attrs['title']
    print(f"音乐名字是:{songname}")
    #   音乐文件地址
    songurl=li[0].find("div", class_="width_s_li").find("a", class_="album_name").attrs['href']
    r=requests.get(url=songurl, headers=headers)
    URL=be(r.text,"lxml").find("div", id="songs", class_="list1").ul.li.a.attrs['href']
    print("URL:",get_video(URL))

if __name__ in "__main__":
    while True:
        print("""
1.输入歌名获取酷狗歌曲
2.qiut退出程序
3.输入: 酷狗热榜 即可获取酷狗热榜歌曲""")
        MySong=input("Input:")
        if MySong == "qiut":
            break
        elif MySong == "酷狗热榜":
            print("\n")
            for k,m in get_phb().items():
                print("\t",k,m)
            print("\n")
        else:
            print("\n")
            search(MySong)
            print("\n")

python提取音频文件中的常见特征 python提取伴奏_python_10

 

python提取音频文件中的常见特征 python提取伴奏_selenium_11

这就是效果和源代码了,其实也不难就是基础的应用。排行榜更新了,也不会影响排行榜模块,其他的就不知道了。