当前位置:   article > 正文

Python3爬虫之咪咕音乐_爬虫 咪咕视频

爬虫 咪咕视频
import re
import time
import json
import requests


class Migu:
    def __init__(self):
        self.url = "http://music.migu.cn/v3/search?keyword={}"
        self.parse_url = "http://music.migu.cn/v3/api/music/audioPlayer/getPlayInfo?copyrightId={}"
        self.headers = {"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.109 Safari/537.36"}
        self.dumps = {'copyrightId': '64046301000'}
        self.count = 0

    def req(self, url):
        # 获取歌曲id 和 歌曲名称
        re_response = requests.get(url, headers=self.headers)
        re_html = re_response.content.decode('utf8')
        re_songid = re.compile('<a href="/v3/music/song/(\d+)" title="(.*?)" target="_blank">.*?</a>', re.S).findall(re_html)
        print('共获取以下歌曲信息:', re_songid)
        return re_songid

    def parse(self, songid):
        url = self.parse_url.format(songid)
        self.dumps['copyrightId'] = songid
        response = requests.get(url, data=json.dumps(self.dumps), headers=self.headers)
        text = json.loads(response.content.decode())
        txt1 = text['walkmanInfo']
        txt = txt1['playUrl']
        # print(txt)
        return txt

    def download(self, txt, song_name):
        self.count = self.count + 1
        with open('%s_%s.mp3' % (song_name, txt), 'wb') as f:
            # 下载mp3文件
            response = requests.get(url=txt)
            f.write(response.content)
            print("下载:%s_%s" % (song_name, txt))
            time.sleep(1)

    def run(self):
        print('请输入要下载的歌曲:')
        text = input()
        print('你输入的歌曲名是:"%s"' % text)
        print('已发送请求!')
        # print(text)
        url = self.url.format(text)
        re_songid = self.req(url)
        for i in re_songid:
            print('暂停3秒~~~')
            time.sleep(3)
            txt = self.parse(i[0])
            print('已获得下载连接!')
            print('暂停3秒~~~')
            time.sleep(3)
            self.download(txt, i[1])


if __name__ == '__main__':
    t1 = Migu()
    t1.run()
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62

以上是通过爬虫咪咕音乐

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/141342
推荐阅读
相关标签
  

闽ICP备14008679号