python爬取网易云音乐热评,爬取网易云音乐热评[附上音乐下载]

[Python] 纯文本查看 复制代码import requests

import time,os

from bs4 import BeautifulSoup

class Music_down:

def __init__(self):

self.headers = {

"User-Agent": "Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/73.0.3683.86 Safari/537.36",

}

self.url = "http://music.163.com/song/media/outer/url?id="

def get_name_byId(self, id):

base_url = "https://music.163.com/song?id={}".format(id)

response = requests.get(base_url, headers=self.headers)

html = BeautifulSoup(response.text, 'lxml')

self.name = html.select('.f-ff2')[0].text

response.close()

self.download(self.name,id)

def download(self, name, id):

#这里出现了下载错误 关于歌的名字问题 不能存在/

# name = name.replace('\xa0', '')

name = name.replace('/', '')

# name = "".join(name.split().remove(" "))

if not os.path.exists('./music'):

os.mkdir('./music')

print("music文件夹创建成功,在当前目录下")

res = requests.get(self.url + id, headers=self.headers)

with open('./music/'+name+'.mp3', 'ab+') as f:

f.write(res.content)

print("*********"+name+"*****下载完成")

def get_music_id(self, urls):

try:

html = requests.get(urls,headers=self.headers).text

except:

print("请输入正确的网址!!!")

return

text = BeautifulSoup(html, 'lxml')

ids = text.select('.f-hide li a')

print("总共%d个文件" % len(ids))

for id in ids:

music_id = id.get('href').split('=')[1]

name = id.text

self.download(name, music_id)

time.sleep(1)

if __name__ == '__main__':

music = Music_down()

while True:

print("""

网易云获取热评:

1. 通过歌曲的id

2. 通过歌单的连接

0. 退出

""")

number = 0

try:

number = int(input("请输入: "))

except:

input("请输入数字")

if number == 2:

song_url = input("请输入歌单的网址: ")

song_url = song_url.replace('/#','')

# print(song_url)

music.get_music_id(song_url)

elif number == 1:

id = input("请输入歌曲的id: ")

music.get_name_byId(id)

elif number == 0:

exit()

else:

print("没有该选项")

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值