网易云音乐爬虫实例(亲测通过)
class Music_down(hotComments): 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('/', '') 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')#BeautifulSoup是用来从HTML或XML中提取数据的Python库。‘lxml’是解析器 ids = text.select('.f-hide li a') print("总共%d个文件" % len(ids)) for id in ids: music_id = id.get('href').split('=')[1]#split切片作用 name = id.text self.download(name, music_id) time.sleep(1)