python 爬蟲 30行代碼帶你爬取酷狗音樂的歌曲內容

在這裏插入圖片描述

#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2020/12/12 10:00
# @Author  : huni
# @File    : 酷狗音樂.py
# @Software: PyCharm
import requests
import warnings
import json
from urllib import parse
warnings.filterwarnings('ignore')
if __name__ == '__main__':
    headers = {
        'referer': 'https://www.kugou.com/',
        'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.183 Safari/537.36'
    }
    url = f'https://complexsearch.kugou.com/v2/search/song?callback=callback123&keyword=%E6%97%A9%E6%99%9A&page=1' \
          f'&pagesize=30&bitrate=0&isfuzzy=0&tag=em&inputtype=0&platform=WebFilter&userid=-1&clientver=2000&iscorrection=1&privilege_filter=0&srcappid=2919&clienttime=1607743429898&mid=1607743429898&uuid=1607743429898&dfid=-&signature=ED386B5C3766E47D47F1252AED823A0F'
    resp = requests.get(url=url, headers=headers).text[12:-2]
    jd = json.loads(resp)  # 字符串轉字典
    for i in jd['data']['lists']:
        a = i['FileHash']
        b = i['AlbumID']
        url1 = f'https://wwwapi.kugou.com/yy/index.php?r=play/getdata&callback' \
              f'=jQuery19108991040761831859_1607742705511&hash=' \
              f'{a}&dfid=334lgQ3gvzHD1503TJ1eRVym&mid=449fc6a9349f6b64fc0d58efab406b8d&platid=4&album_id' \
               f'={b}&_=1607742705512'
        resp1 = requests.get(url=url1,headers=headers).text[41:-2]
        jd1 = json.loads(resp1)       # 字符串轉字典
        print('歌名',jd1['data']['audio_name'])
        print('專輯名',jd1['data']['album_name'])
        print('歌曲連接',jd1['data']['play_url'])
複製代碼

今天的分享就到這,若是你對Python感興趣,歡迎加入咱們【python學習交流裙】,免費領取學習資料和源碼。php

相關文章
相關標籤/搜索