python爬取网易云音乐歌单音乐
在网易云音乐中第一页歌单的url:http://music.163.com/#/discover/playlist/
依次第二页:http://music.163.com/#/discover/playlist/?order=hot&cat=%E5%85%A8%E9%83%A8&limit=35&offset=35
依次第三页:http://music.163.com/#/discover/playlist/?order=hot&cat=%E5%85%A8%E9%83%A8&limit=35&offset=70
然后从歌单的查看框架的源代码:
从图中的源代码可以得到每个歌单的url:eg:http://music.163.com/#/playlist?id=696806036
然后相应的歌单页面中可以得到歌单中每首歌的名字和歌手:
然后歌名歌手,在百度音乐搜索api接口获得songid,api是url = "http://sug.music.baidu.com/info/suggestion"
截图来自:贴吧
然后从获取到的songid从百度音乐免费API接口:http://music.baidu.com/data/music/fmlink,获取songLink进行下载,并且将songLink保存到本地.flac
eg;http://music.baidu.com/data/music/fmlink?rate=320&songIds=242078437&type=flac 下载结果:
代码:
# -*- coding: utf-8 -*-
import re
import urllib
import urllib2
import os
import stat
import itertools
import re
import sys
import requests
import json
import time
import socket
import urlparse
import csv
import random
from datetime import datetime, timedelta
import lxml.html from zipfile import ZipFile
from StringIO import StringIO
from downloader import Downloader
from bs4 import BeautifulSoup
from HTMLParser import HTMLParser
from itertools import product
import sys
reload(sys)
sys.setdefaultencoding('utf8')
URL = 'http://music.163.com'
NUM = 5
def download(url, user_agent='wswp', num_try=2): headers = {'User_agent': user_agent}
request = urllib2.Request(url, headers=headers)
try:
html = urllib2.urlopen(request).read()
except urllib2.URLError as e:
print 'Download error', e.reason
html = None
if num_try > 0:
if hasattr(e, 'code') and 500 <= e.code < 600:
return download(url, user_agent, num_try - 1)
return html def get_song_list(url):
html = download(url)
res = r'<ul class="f-hide">(.*?)</ul>'
mm = re.findall(res,html,re.S | re.M)
#print mm
res = r'<li><a .*?>(.*?)</a></li>'
song_list = re.findall(res, html,re.S | re.M)
return song_list #获取网易云歌单 eg:/playlist?id=706469943
def get_play_list(html):
soup = BeautifulSoup(html, "html.parser")
results = soup.find_all(name='a', attrs={'class': 'tit f-thide s-fc0'})
list = []
for each in results:
ee = each.get('href')
list.append(ee)
return list def download_music(url, song_name):
print "Downloading song_name:" + song_name
path = "songs"
if not os.path.isdir(path):
os.mkdir(path)
f = open(path + '/' + song_name + '.flac', 'wb')
f.write(download(url))
f.close() def download_song(song_name,singer): url = "http://sug.music.baidu.com/info/suggestion"
#百度音乐搜索获得songid
mess = song_name + singer
payload = {'word': mess, 'version': '2.1.1', 'from': ''}
r = requests.get(url, params=payload)
contents = r.text
d = json.loads(contents, encoding="utf-8")
#print d
if ('data' not in d):
print "do not have flac"
return 0
if ('song' not in d["data"]):
print "do not have flac"
return 0
song_id = d["data"]["song"][0]["songid"] print "song_id:"+song_id url = "http://music.baidu.com/data/music/fmlink" #百度音乐免费api接口
'''
http://music.baidu.com/data/music/fmlink?rate=320&songIds=242078437&type=&callback=cb_download&_t=1468380564513&format=json
'''
payload = {'songIds': song_id, 'type': 'mp3'}
r = requests.get(url, params=payload)
contents = r.text
try:
d = json.loads(contents, encoding="utf-8")
except:
return 0
if d is not None and 'data' not in d or d['data'] == '':
return 0
songlink = d["data"]["songList"][0]["songLink"]
if (len(songlink) < 10):
print "do not have flac"
return 0
print "Song Source: " + songlink
download_music(songlink,mess) def get_song_singer(url):
html = download(url)
soup = BeautifulSoup(html, "html.parser")
results = soup.find_all(name='textarea', attrs={'style': 'display:none;'})
mess = str(results[0])
tt = len('<textarea style="display:none;">')
result = mess[tt:]
tt = len('</textarea>)')-1
resu = result[:-tt]
list = json.loads(resu, encoding="utf-8")
singer_list = []
for each in list:
singer_list.append(each["artists"][0]["name"])
return singer_list if __name__ == '__main__': num = 0
for flag in range(1,5):
if flag > 1:
page = (flag - 1) * 35
url = 'http://music.163.com/discover/playlist/?order=hot&cat=%E5%85%A8%E9%83%A8&limit=35&offset='+str(page)
else:
url = 'http://music.163.com/discover/playlist'
print url
html = download(url)
list = get_play_list(html)
for i in list:
song_list_url = URL + i
print song_list_url
singer_list = get_song_list(song_list_url)
singer_name = get_song_singer(song_list_url)
tt = len(singer_list)
mm = len(singer_name)
index = min(tt,mm)
num = num + mm
for j in range(0, index):
print singer_name[j]
print singer_list[j]
download_song(singer_list[j],singer_name[j])
print "\n" print "Download " + str(num) + " music\n"
python爬取网易云音乐歌单音乐的更多相关文章
- Python爬取网易云热歌榜所有音乐及其热评
获取特定歌曲热评: 首先,我们打开网易云网页版,击排行榜,然后点击左侧云音乐热歌榜,如图: 关于如何抓取指定的歌曲的热评,参考这篇文章,很详细,对小白很友好: 手把手教你用Python爬取网易云40万 ...
- 爬虫实战(二) 用Python爬取网易云歌单
最近,博主喜欢上了听歌,但是又苦于找不到好音乐,于是就打算到网易云的歌单中逛逛 本着 "用技术改变生活" 的想法,于是便想着写一个爬虫爬取网易云的歌单,并按播放量自动进行排序 这篇 ...
- Python爬取网易云音乐歌手歌曲和歌单
仅供学习参考 Python爬取网易云音乐网易云音乐歌手歌曲和歌单,并下载到本地 很多人学习python,不知道从何学起.很多人学习python,掌握了基本语法过后,不知道在哪里寻找案例上手.很多已经做 ...
- 用Python爬取网易云音乐热评
用Python爬取网易云音乐热评 本文旨在记录Python爬虫实例:网易云热评下载 由于是从零开始,本文内容借鉴于各种网络资源,如有侵权请告知作者. 要看懂本文,需要具备一点点网络相关知识.不过没有关 ...
- Python爬取网易云歌单
目录 1. 关键点 2. 效果图 3. 源代码 1. 关键点 使用单线程爬取,未登录,爬取网易云歌单主要有三个关键点: url为https://music.163.com/discover/playl ...
- Python 爬取网易云歌手的50首热门作品
使用 requests 爬取网易云音乐 Python 代码: import json import os import time from bs4 import BeautifulSoup impor ...
- python爬取网易云音乐歌曲评论信息
网易云音乐是广大网友喜闻乐见的音乐平台,区别于别的音乐平台的最大特点,除了“它比我还懂我的音乐喜好”.“小清新的界面设计”就是它独有的评论区了——————各种故事汇,各种金句频出.我们可以透过歌曲的评 ...
- python爬取网易云周杰伦所有专辑,歌曲,评论,并完成可视化分析
---恢复内容开始--- 去年在网络上有一篇文章特别有名:我分析42万字的歌词,为搞清楚民谣歌手们在唱些什么.这篇文章的作者是我大学的室友,随后网络上出现了各种以为爬取了XXX,发现了XXX为名的文章 ...
- 如何用Python网络爬虫爬取网易云音乐歌曲
今天小编带大家一起来利用Python爬取网易云音乐,分分钟将网站上的音乐down到本地. 跟着小编运行过代码的筒子们将网易云歌词抓取下来已经不再话下了,在抓取歌词的时候在函数中传入了歌手ID和歌曲名两 ...
随机推荐
- 【BZOJ2216】Lightning Conductor(动态规划)
[BZOJ2216]Lightning Conductor(动态规划) 题面 BZOJ,然而是权限题 洛谷 题解 \(\sqrt {|i-j|}\)似乎没什么意义,只需要从前往后做一次再从后往前做一次 ...
- CF954F Runner's Problem(动态规划,矩阵快速幂)
CF954F Runner's Problem(动态规划,矩阵快速幂) 题面 CodeForces 翻译: 有一个\(3\times M\)的田野 一开始你在\((1,2)\)位置 如果你在\((i, ...
- scala(一)
一.Scala 简介 1.Scala语言既可用于大规模应用程序开发,也可以用于脚本编程,2001年由Martin Odersk 开发,主要优势 速度和它的表达性.一门函数式编程语言,既有面向对象的特点 ...
- 【CF601C】Kleofáš and the n-thlon
Portal -->CF601C Description 大概是说\(m\)个人参加\(n\)场比赛,每场一人有一个排名,每场没有两个人排名相同,一个人最后的得分是\(n\)场比赛的排名相加,现 ...
- 使用无线网卡搭建虚拟wifi
1.首先以管理员身份运行命令提示符 开始->搜索框输入cmd,出来的cmd.exe上右键管理员身份运行,或者win+R打开运行提示框,输入cmd并回车. 2.设置“虚拟Wifi网卡”模式 敲入命 ...
- win32/linux 线程 log
原文 #include <stdio.h> #include <stdlib.h> #include <string.h> #ifdef WIN32 #includ ...
- struts标签错误:Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/core"
今天使用eclipse开发ssh,出现Can not find the tag library descriptor for "http://java.sun.com/jsp/jstl/co ...
- fmt:formatNumber use locale display negative currency in -$xxx.xx format in JSTL
First, we want to know our own locale,how to display the locale in a JSTL? <c:out value="${p ...
- ZOJ 3782 G - Ternary Calculation 水
LINK:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=3782 题意:给出3个数和两个符号(+-*/%) 思路:拿到题目还 ...
- Java 里快如闪电的线程间通讯
这个故事源自一个很简单的想法:创建一个对开发人员友好的.简单轻量的线程间通讯框架,完全不用锁.同步器.信号量.等待和通知,在Java里开发一个轻量.无锁的线程内通讯框架:并且也没有队列.消息.事件或任 ...