欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

基于Python实现下载网易音乐代码实例

程序员文章站 2022-03-07 08:41:35
代码如下# 爬取网易音乐import requestsfrom bs4 import beautifulsoupimport urllib.requestheaders = {"origin": "h...

代码如下

# 爬取网易音乐
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实现下载网易音乐代码实例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。