⽹易云⾳乐爬⾍代码⽰例⽹易云⾳乐爬⾍代码⽰例
以下是代码⽰例
import os
import requests
from bs4 import BeautifulSoup
url ='music.163/playlist?id=5166962280'
headers ={
'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:80.0) Gecko/20100101 Firefox/80.0'
}
if not ists('./⽹易云歌单/'):
os.mkdir('./⽹易云歌单/')
# 保存了cook值
session = requests.session()
response = (url, headers=headers).content
# print(response)
# 筛选数据
soup = BeautifulSoup(response,'lxml')
music_data = soup.find('ul', class_ ='f-hide')
# print(music_data)
# 对数据进⾏处理,只保留歌曲名称
url_lists =[]
for music in music_data.find_all('a'):
list=[]
# print('{}:{}'., music['href']))
music_url ='music.163/song/media/outer/url'+music['href'][5:]+'.mp3'
music_name =
# print(f'{music_name}:{music_url}')
list.append(music_name)
list.append(music_url)
url_lists.append(list)
# print(url_lists)
# 下载
for i in url_lists:
name = i[0]
url = i[1]
try:
print(f'正在下载{name}')
images_response = (url, stream=True)
path ='./⽹易云歌单/'+ name +'.mp3'
with open(path,'wb')as f:
for chunk in images_response.iter_content(chunk_size=128):
f.write(chunk)
except:
html播放音乐代码continue
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论