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

歡迎訪問 生活随笔!

生活随笔

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

python

python 爬虫 音乐下载 歌手_python爬虫批量下载全民K歌音乐

發布時間:2023/11/30 python 36 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python 爬虫 音乐下载 歌手_python爬虫批量下载全民K歌音乐 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

標簽:爬蟲

網址示例: https://node.kg.qq.com/personal?uid=639e9983222a338a

直接上源碼:

import requests

import time

import re

import json

import pprint

import math

import os

header={

'User-Agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36 SE 2.X MetaSr 1.0'

}

SongList=[]

song_baseurl="http://node.kg.qq.com/play"

album_baseurl="http://node.kg.qq.com/cgi/fcgi-bin/kg_ugc_get_homepage"

def Down(url_file, filePath, FileDir):

if not os.path.isdir(FileDir): os.makedirs(FileDir)

if os.path.isfile(FileDir + "/" + filePath):

print(filePath + " --已存在")

return 0

try:

r = requests.get(url_file, stream=True)

with open(FileDir + "/" + filePath, "wb") as f:

size = int(r.headers['content-length'])

title = " 當前下載-" + filePath + " 文件大小:" + size + "字節"

print('\033[0;31m' + title + "\033[0m")

CurTotal = 0

for chunk in r.iter_content(chunk_size=512 * 1024):

if chunk:

f.write(chunk)

CurTotal += len(chunk)

print("\r" + filePath + "--下載進度:" + '%3s' % (str(CurTotal * 100 // size)) + "%", end='')

print()

r.close()

except Exception as e:

print(filePath + " 下載出錯!" + " 錯誤信息" + str(e.args))

if os.path.isfile(FileDir + "/" + filePath): os.remove(FileDir + "/" + filePath)

def GetData(data,url):

response = requests.get(url,params=data, headers=header)

return response.content.decode("utf-8")

def Parse_Song_Info(content):

jsonobj = re.findall(r'window.__DATA__ = (.*?); ', content)

if len(jsonobj) > 0:

data = json.loads(jsonobj[0])

# pprint.pprint(data)

obj={}

obj[ "name"]=data['detail']['song_name']

if data['detail']['playurl']:

print(data['detail']['song_name']+" 音樂:"+data['detail']['playurl'])

obj["url"]=data['detail']['playurl']

obj["type"]=".mp3"

else:

print(data['detail']['song_name'] + " 視頻:" + data['detail']['playurl_video'])

obj["url"] = data['detail']['playurl_video']

obj["type"] = ".mp4"

SongList.append(obj)

else:

print("沒有爬取到")

def GetSongsByIndex(uid, Is_Parse, page):

data={

'jsonpCallback':'callback_0',

'g_tk':'5381',

'outCharset':'utf-8',

'format':'jsonp',

'type':'get_ugc',

'start':str(page),

'num':'8',

'touin':'',

'share_uid':uid,

'g_tk_openkey':'5381',

'_':str(int(time.time()*1000))

}

response=requests.get(album_baseurl,params=data,headers=header)

jsonobj=re.findall(r'callback_\d\((.*)\)',response.content.decode("utf-8"))

if len(jsonobj)>0:

data=json.loads(jsonobj[0])

# pprint.pprint(data)

count=data['data']['ugc_total_count']

if Is_Parse:

time.sleep(1)

for obj in data['data']['ugclist']:

print(obj['title']+" -- "+obj['shareid'])

songdata = {

"s": obj['shareid']

}

content=GetData(songdata,song_baseurl)

Parse_Song_Info(content)

else:

pprint.pprint("共計:" + str(count))

return count

else:

print("沒有爬取到")

return 0

def Run(uid):

count=GetSongsByIndex(uid, False, 1)

if count!=0:

for page in range(1,math.ceil(count/8)+1):

GetSongsByIndex(uid, True, page)

else:

print("該用戶沒有歌曲")

if __name__=="__main__":

Run('639e9983222a338a')

for s in SongList:

Down(s["url"],s["name"]+s["type"],"小小")

# print(s)

將Run 括號里面的字符串換為 歌手主頁鏈接后面的uid

“小小” 可自定義文件夾

標簽:爬蟲

來源: https://www.cnblogs.com/yuanzessrs/p/10247347.html

總結

以上是生活随笔為你收集整理的python 爬虫 音乐下载 歌手_python爬虫批量下载全民K歌音乐的全部內容,希望文章能夠幫你解決所遇到的問題。

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