python爬取網(wǎng)易云音樂(lè)熱歌榜實(shí)例代碼
首先找到要下載的歌曲排行榜的鏈接,這里用的是:
https://music.163.com/discover/toplist?id=3778678
然后更改你要保存的目錄,目錄要先建立好文件夾,例如我的是保存在D盤(pán)-360下載-網(wǎng)易云熱歌榜文件夾內(nèi),就可以完成下載。
如果文件夾沒(méi)有提前建好,會(huì)報(bào)錯(cuò)[Errno 2] No such file or directory。
代碼實(shí)現(xiàn):
from urllib import request
from bs4 import BeautifulSoup
import re
import requests
import time
class Music(object):
def __init__(self, baseurl, path):
head = {
"user-agent": "Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36"
}
self.baseurl = baseurl
self.headers = head
self.path = path
def main(self):
html = self.askurl()
bs4 = self.analysis(html)
name1 = self.matching(bs4)
self.save(name1)
def askurl(self):
req = request.Request(url=self.baseurl, headers=self.headers)
response = request.urlopen(req)
html = response.read().decode("utf-8")
return html
def analysis(self, html):
soup = BeautifulSoup(html, "html.parser")
bs4 = soup.find_all("textarea")
bs4 = str(bs4)
return bs4
def matching(self, bs4):
rule0 = re.compile(r'"name":"(.*?)","tns":[],"alias":[]')
name0 = re.findall(rule0, bs4)
str = ""
for i in name0:
str = str + "," + i
str = str.replace("\xa0", " ")
rule1 = re.compile(r'jpg,(.*?),(.*?)","id":(\d*)')
name1 = re.findall(rule1, str)
return name1
def save(self, name1):
for j in name1:
print("正在下載:" + j[1] + " - " + j[0] + "...")
url = "http://music.163.com/song/media/outer/url?id=" + j[2]
content = requests.get(url=url, headers=self.headers).content
with open(self.path + j[1] + " - " + j[0] + ".mp3", "wb") as f:
f.write(content)
print(j[1] + " - " + j[0] + "下載完畢。\n")
time.sleep(0.5)
return
if __name__ == "__main__":
baseurl = "https://music.163.com/discover/toplist?id=3778678" # 要爬取的熱歌榜鏈接
path = "D:/360下載/網(wǎng)易云熱歌榜/" # 保存的文件目錄
demo0 = Music(baseurl, path)
demo0.main()
print("下載完畢")
內(nèi)容擴(kuò)展:
Python3實(shí)戰(zhàn)之爬蟲(chóng)抓取網(wǎng)易云音樂(lè)的熱門(mén)評(píng)論
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
import re
import urllib.request
import urllib.error
import urllib.parse
import json
def get_all_hotSong(): #獲取熱歌榜所有歌曲名稱和id
url='http://music.163.com/discover/toplist?id=3778678' #網(wǎng)易云云音樂(lè)熱歌榜url
html=urllib.request.urlopen(url).read().decode('utf8') #打開(kāi)url
html=str(html) #轉(zhuǎn)換成str
pat1=r'<ul class="f-hide"><li><a href="/song\?id=\d*?" rel="external nofollow" rel="external nofollow" >.*</a></li></ul>' #進(jìn)行第一次篩選的正則表達(dá)式
result=re.compile(pat1).findall(html) #用正則表達(dá)式進(jìn)行篩選
result=result[0] #獲取tuple的第一個(gè)元素
pat2=r'<li><a href="/song\?id=\d*?" rel="external nofollow" rel="external nofollow" >(.*?)</a></li>' #進(jìn)行歌名篩選的正則表達(dá)式
pat3=r'<li><a href="/song\?id=(\d*?)" rel="external nofollow" >.*?</a></li>' #進(jìn)行歌ID篩選的正則表達(dá)式
hot_song_name=re.compile(pat2).findall(result) #獲取所有熱門(mén)歌曲名稱
hot_song_id=re.compile(pat3).findall(result) #獲取所有熱門(mén)歌曲對(duì)應(yīng)的Id
return hot_song_name,hot_song_id
def get_hotComments(hot_song_name,hot_song_id):
url='http://music.163.com/weapi/v1/resource/comments/R_SO_4_' + hot_song_id + '?csrf_token=' #歌評(píng)url
header={ #請(qǐng)求頭部
'User-Agent':'Mozilla/5.0 (X11; Fedora; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36'
}
#post請(qǐng)求表單數(shù)據(jù)
data={'params':'zC7fzWBKxxsm6TZ3PiRjd056g9iGHtbtc8vjTpBXshKIboaPnUyAXKze+KNi9QiEz/IieyRnZfNztp7yvTFyBXOlVQP/JdYNZw2+GRQDg7grOR2ZjroqoOU2z0TNhy+qDHKSV8ZXOnxUF93w3DA51ADDQHB0IngL+v6N8KthdVZeZBe0d3EsUFS8ZJltNRUJ','encSecKey':'4801507e42c326dfc6b50539395a4fe417594f7cf122cf3d061d1447372ba3aa804541a8ae3b3811c081eb0f2b71827850af59af411a10a1795f7a16a5189d163bc9f67b3d1907f5e6fac652f7ef66e5a1f12d6949be851fcf4f39a0c2379580a040dc53b306d5c807bf313cc0e8f39bf7d35de691c497cda1d436b808549acc'}
postdata=urllib.parse.urlencode(data).encode('utf8') #進(jìn)行編碼
request=urllib.request.Request(url,headers=header,data=postdata)
reponse=urllib.request.urlopen(request).read().decode('utf8')
json_dict=json.loads(reponse) #獲取json
hot_commit=json_dict['hotComments'] #獲取json中的熱門(mén)評(píng)論
num=0
fhandle=open('./song_comments','a') #寫(xiě)入文件
fhandle.write(hot_song_name+':'+'\n')
for item in hot_commit:
num+=1
fhandle.write(str(num)+'.'+item['content']+'\n')
fhandle.write('\n==============================================\n\n')
fhandle.close()
hot_song_name,hot_song_id=get_all_hotSong() #獲取熱歌榜所有歌曲名稱和id
num=0
while num < len(hot_song_name): #保存所有熱歌榜中的熱評(píng)
print('正在抓取第%d首歌曲熱評(píng)...'%(num+1))
get_hotComments(hot_song_name[num],hot_song_id[num])
print('第%d首歌曲熱評(píng)抓取成功'%(num+1))
num+=1
以上就是python爬取網(wǎng)易云音樂(lè)熱歌榜實(shí)例代碼的詳細(xì)內(nèi)容,更多關(guān)于python爬取網(wǎng)易云音樂(lè)熱歌榜的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 實(shí)現(xiàn)在txt指定行追加文本的方法
下面小編就為大家分享一篇python 實(shí)現(xiàn)在txt指定行追加文本的方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-04-04
Python爬蟲(chóng)爬取百度搜索內(nèi)容代碼實(shí)例
這篇文章主要介紹了Python爬蟲(chóng)爬取百度搜索內(nèi)容代碼實(shí)例,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-06-06
利用python實(shí)現(xiàn)周期財(cái)務(wù)統(tǒng)計(jì)可視化
這篇文章主要給大家介紹了關(guān)于如何利用python實(shí)現(xiàn)周期財(cái)務(wù)統(tǒng)計(jì)可視化的相關(guān)資料,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用python具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-08-08
使用python求解迷宮問(wèn)題的三種實(shí)現(xiàn)方法
關(guān)于迷宮問(wèn)題,常見(jiàn)會(huì)問(wèn)能不能到達(dá)某點(diǎn),以及打印到達(dá)的最短路徑,下面這篇文章主要給大家介紹了關(guān)于如何使用python求解迷宮問(wèn)題的三種實(shí)現(xiàn)方法,需要的朋友可以參考下2022-03-03
Python實(shí)現(xiàn)復(fù)雜對(duì)象轉(zhuǎn)JSON的方法示例
這篇文章主要介紹了Python實(shí)現(xiàn)復(fù)雜對(duì)象轉(zhuǎn)JSON的方法,結(jié)合具體實(shí)例形式分析了Python針對(duì)json轉(zhuǎn)換的相關(guān)操作技巧,需要的朋友可以參考下2017-06-06
使用Nginx+uWsgi實(shí)現(xiàn)Python的Django框架站點(diǎn)動(dòng)靜分離
這篇文章主要介紹了使用Nginx+uWsgi實(shí)現(xiàn)Python的Django框架站點(diǎn)動(dòng)靜分離的部署實(shí)例,即靜態(tài)由Nginx處理而Python頁(yè)面由Django自帶的HTTP服務(wù)器處理,需要的朋友可以參考下2016-03-03
Python3中FuzzyWuzzy庫(kù)實(shí)例用法
在本篇文章中小編給各位整理了關(guān)于Python3z中FuzzyWuzzy庫(kù)實(shí)例用法及相關(guān)代碼,有興趣的朋友們可以參考下。2020-11-11

