欢迎光临
我们一直在努力

爬取网易云音乐热评[附上音乐下载]

声明是转载的:

这个代码 可以通过歌曲的id, 或者是歌单的链接,
来爬网易云音乐的热评
需要的包有 bs4, requests
pip install 包名进行安装

import requests
import json
from bs4 import BeautifulSoup
import time
 
class 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",
        }
    #offset页数,limit一页显示最多的评论数
    def get_hotComments(self,music_id):
        self.url = "http://music.163.com/api/v1/resource/comments/R_SO_4_{}?offset=0&limit=50".format(music_id)
        try:
            self.text = json.loads(requests.get(self.url, headers=self.headers).text)
            datas = self.text['hotComments']
        except:
            print("没有找到该歌曲的热评")
            return
        print("\n\n\n"+"网易云热评:   "+"--------"*20+"\n\n\n")
        for content in datas:
            print(content['content'])
        print("\n\n\n"+"---分--界--线----"*20)
 
    def get_music_id(self, urls=None):
        try:
            html = requests.get(urls,headers=self.headers).text
        except:
            print("请输入正确的网址!!!")
            return
        text = BeautifulSoup(html, 'lxml')
        ids = text.select('.f-hide li a')
        # print(text)
        for id in ids:
            music_id = id.get('href').split('=')[1]
            self.get_hotComments(music_id)
            time.sleep(1)
 
if __name__ == '__main__':
    music = hotComments()
    while True:
        print("""
        网易云获取热评:
            1. 通过歌曲的id
            2. 通过歌单的连接
            0. 退出
        """)
        number = 0
        try:
            number = int(input("请输入: "))
        except:
            input("请输入数字")
        if number == 2:
            song_url = input("请输入歌单的网址: ")
            song_url = song_url.replace('/#','')
            # print(song_url)
            music.get_music_id(urls=song_url)
        elif number == 1:
            id = input("请输入歌曲的id: ")
            music.get_hotComments(id)
        elif number == 0:
            exit()
        else:
            print("没有该选项")

下面的是网易云音乐下载的代码

import requests
import time,os
from bs4 import BeautifulSoup
class Music_down:
    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('\xa0', '')
        name = name.replace('/', '')
        # name = "".join(name.split().remove(" "))
        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')
        ids = text.select('.f-hide li a')
        print("总共%d个文件" % len(ids))
 
        for id in ids:
            music_id = id.get('href').split('=')[1]
            name = id.text
            self.download(name, music_id)
            time.sleep(1)
if __name__ == '__main__':
    music = Music_down()
    while True:
        print("""
        网易云获取热评:
            1. 通过歌曲的id
            2. 通过歌单的连接
            0. 退出
        """)
        number = 0
        try:
            number = int(input("请输入: "))
        except:
            input("请输入数字")
        if number == 2:
            song_url = input("请输入歌单的网址: ")
            song_url = song_url.replace('/#','')
            # print(song_url)
            music.get_music_id(song_url)
        elif number == 1:
            id = input("请输入歌曲的id: ")
            music.get_name_byId(id)
        elif number == 0:
            exit()
        else:
            print("没有该选项")
   

如果文章对你有帮助,欢迎点击上方按钮打赏作者

未经允许不得转载:军哥驿站 » 爬取网易云音乐热评[附上音乐下载]

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

军哥驿站 建站更专业 更方便

网站开发联系我们