import requests
from bs4 import BeautifulSoup
headers={'user-agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/83.0.4103.106 Safari/537.36'}
res=requests.get('https://music.163.com/discover/toplist?id=3779629',headers=headers)
soup=BeautifulSoup(res.text,'html.parser')
inf=soup.find('ul',class_="f-hide")
infor=inf.find_all('a')
list=[]
namelist=[]
number=0
for i in infor:
number=number+1
name=i.text
web=i.get('href')
id=web[9:]
url='https://music.163.com'+web
list.append(id)
namelist.append(name)
print(str(number)+' '+name+' '+url)
def download():
while True:
a=input('请输入您要下载歌曲的序号')
b=int(a)-1
aa=list[int(b)]
fn=namelist[int(b)]
p='http://music.163.com/song/media/outer/url?id={}.mp3'.format(aa)
music=requests.get(p,headers=headers).content
with open('{}.mp3'.format(fn),'wb') as f:
f.write(music)
f.close()
print(fn+' 下载完毕')
download()
爬虫新手,水平有限,大家轻喷꒰๑• ̫•๑꒱ ♡
转载:https://blog.csdn.net/weixin_47128538/article/details/106853638
查看评论