python爬虫入门 ✦ 爬取下载网易云音乐

此博客仅为我业余记录文章所用,发布到此,仅供网友阅读参考,如有侵权,请通知我,我会删掉。

补充

  • 该博文是2019.08写的

文章配套了视频,结合视频食用,味道更佳。点击直达!


1.完整代码

代码直接复制黏贴即可使用,注意!!!需要安装selenium

# -*- coding:utf-8 -*-
# author   : SunriseCai
# datetime : 2020/3/27 18:12
# software : PyCharm

import re
import requests
from selenium import webdriver


class DownloadMusic(object):
    def __init__(self):
        self.music_api = 'http://music.163.com/song/media/outer/url?id={}.mp3'
        self.music_search = 'https://music.163.com/#/search/m/?s={}'
        self.headers = {
            'User-Agent': 'Mozilla/5.0'
        }
        chrome_options = webdriver.ChromeOptions()
        chrome_options.add_argument('--headless')
        self.browser = webdriver.Chrome(options=chrome_options)

    def __del__(self):
        """
        运行结束自动关闭webdriver
        :return:
        """
        self.browser.close()

    def search_music(self, param):
        """
        传入搜索关键字,并请求网页
        :param param: 搜索关键字
        :return:
        """
        url = self.music_search.format(param)
        self.browser.get(url)
        # 值得注意的是这里需要跳转iframe,不懂请自行百度
        self.browser.switch_to_frame("g_iframe")
        html = self.browser.page_source
        self.parse_html(html)

    def parse_html(self, html):
        """
        传入html,解析网页
        :param html: 
        :return:
        """
        song_id = re.findall('a href="/song\?id=(.*?)"', html)
        song_name = re.findall(('), html)
        singer = re.findall('span class="s-fc7">(.*?)', html)
        self.download_music(song_id, song_name, singer)

    def download_music(self, song_id, song_name, singer):
        """
        传入歌曲id,歌曲名,歌手并下载歌曲
        :param song_id: 
        :param song_name: 
        :param singer: 
        :return:
        """
        for id, name, singer in zip(song_id, song_name, singer):
            song_url = self.music_api.format(id)
            resp = requests.get(song_url)
            with open('%s-%s.mp3' % (singer, name), 'wb') as f:
                f.write(resp.content)
                print('已下载 歌曲:%s-%s.mp3' % (singer, name))
                f.close()

    def main(self, param):
        self.search_music(param)


if __name__ == '__main__':
    spider = DownloadMusic()
    param = input("请输入歌手或歌名:")
    spider.main(param)

你可能感兴趣的:(Python爬虫入门)