python爬虫---爬取网易云音乐
代码:
import requests from lxml import etree text = requests.get("https://music.163.com/discover/toplist?id=3778678").text html = etree.HTML(text) id_list = html.xpath("//a[contains(@href,'/song?')]") for i in id_list: href = i.xpath("./@href")[0] music_id = href.split("=")[1] music_url = "https://music.163.com/song%s?id=%s"%("/media/outer/url",music_id) # 歌曲名称 music_name = i.xpath("./text()")[0] # 访问歌曲 data = requests.get(music_url) # 下载歌曲 if data.status_code == 200: with open("./music/%s.mp3" % music_name, "wb") as file: file.write(data.content) print(music_name)