码迷,mamicode.com
首页 > 编程语言 > 详细

Python-网易音乐下载

时间:2020-05-19 18:40:16      阅读:107      评论:0      收藏:0      [点我收藏+]

标签:mic   music   response   get   ike   span   res   http   curl   

# 爬取网易音乐
import requests
from bs4 import BeautifulSoup
import urllib.request

headers = {"origin": "https://music.163.com",
            "referer": "https://music.163.com/",
            "user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 Safari/537.36"}

url = "http://music.163.com/playlist?id=2182968685"
response = requests.get(url, headers=headers).content

resp = BeautifulSoup(response, "lxml")
mains = resp.find("ul", {"class": "f-hide"})

lists = []
for music in mains.find_all("a"):
    list = []
    # 拼装下载地址
    musicUrl = http://music.163.com/song/media/outer/url + music[href][5:] + .mp3
    musicname = music.text
    list.append(musicUrl)
    list.append(musicname)
    lists.append(list)
for i in lists:
    url = i[0]
    name = i[1]
    try:
        print("正在下载:{}".format(name))
        urllib.request.urlretrieve(url, "D:\脚本项目\lianxi\MP3\%s.mp3"%name)
    except:
        print("下载失败")

结果展示

技术图片

 

Python-网易音乐下载

标签:mic   music   response   get   ike   span   res   http   curl   

原文地址:https://www.cnblogs.com/zhouzetian/p/12918257.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!