日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

Python爬虫案例:爬取酷狗音乐全排行榜歌曲

發布時間:2023/12/29 python 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Python爬虫案例:爬取酷狗音乐全排行榜歌曲 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

前言

本文的文字及圖片來源于網絡,僅供學習、交流使用,不具有任何商業用途,版權歸原作者所有,如有問題請及時聯系我們以作處理

本次目標

爬取酷狗音樂全站排行榜歌曲

?

?

目標地址

https://www.kugou.com/yy/html/rank.html?from=homepage

環境

Python3.6.5

pycharm

?

爬蟲代碼

調入工具

import requests import re import parsel

請求網站

headers = {'authority': 'wwwapi.kugou.com','cookie': 'kg_mid=ac3836df72c523f46a85d8a5fd90fe59; kg_dfid=3ve7aQ2XyGmN0yE3uv3WcaHs; Hm_lvt_aedee6983d4cfc62f509129360d6bb3d=1600260110,1602312707; kg_dfid_collect=d41d8cd98f00b204e9800998ecf8427e; kg_mid_temp=ac3836df72c523f46a85d8a5fd90fe59; Hm_lpvt_aedee6983d4cfc62f509129360d6bb3d=1602312738','referer': 'https://www.kugou.com/song/','user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36', } url = 'https://www.kugou.com/yy/html/rank.html' response = requests.get(url=url, headers=headers)

解析網站數據

def func(url):response = requests.get(url=url, headers=headers)response.encode = response.apparent_encodinghashs = re.findall('"Hash":"(.*?)"', response.text, re.S)album_ids = re.findall('"album_id":(.*?),"', response.text, re.S)FileNames = re.findall('"FileName":"(.*?)"', response.text, re.S)data = zip(hashs, album_ids, FileNames)for i in data:hash = i[0]album_ids = i[1]FileName = i[2].encode('utf-8').decode('unicode_escape')# print(hash, album_ids, FileName)download_url = 'https://wwwapi.kugou.com/yy/index.php'params = {'r': 'play/getdata','callback': 'jQuery19107150201841602037_1602314563329','hash': '{}'.format(hash),'album_id': '{}'.format(album_ids),'dfid': '3ve7aQ2XyGmN0yE3uv3WcaHs','mid': 'ac3836df72c523f46a85d8a5fd90fe59','platid': '4','_': '1602312793005',}for i in html_data:page_url = i[0]name = i[1]print(page_url)func(page_url)print('==========================正在爬取{}歌曲========================'.format(name))

保存數據

def download(url, title):filename = '保存地址' + title + '.mp3'response = requests.get(url=url, headers=headers)with open(filename, mode='wb') as f:f.write(response.content)print(title)

運行代碼,效果如下圖

?

總結

以上是生活随笔為你收集整理的Python爬虫案例:爬取酷狗音乐全排行榜歌曲的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。