爬取网易云音乐热歌榜
import requests
from bs4 import BeautifulSoup
header = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36 Edg/80.0.361.66',
}
url = "https://music.163.com/discover/toplist?id=3778678"
response = requests.get(url, headers=header)
html = response.content
soup = BeautifulSoup(html, "html.parser")
songs = soup.find("ul", class_="f-hide").select("a")
i = 1
for song in songs:
song_id = song['href'][9:]
song_name = song.text
song_down_url = "http://music.163.com/song/media/outer/url?id=" + song_id + ".mp3"
print("第 " + str(i) + " 首歌曲:" + song_down_url)
print("正在下载...")
response = requests.get(song_down_url, headers=header).content
f = open(r'F:/Python大作业/网易云音乐/'+song_name + ".mp3", 'wb')
f.write(response)
f.close()
print("下载完成.\n\r")
i = i + 1