网易云音乐爬虫实例(亲测通过)

上传:management4079 浏览: 19 推荐: 0 文件:.py 大小:4.43 KB 上传时间:2021-05-19 18:10:06 版权申诉

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)

上传资源
用户评论