思路
一开始的想法是全都直接根据歌单获取歌曲的ID,然后每首歌都按那个格式001.mp3\001.txt\001.png存下来
后来发现我好像做不到直接下mp3……因为在网页版一点会跳出来请在PC版下载(咦所以结果我也不知道怎么操作……)

然后计划就变成了 - 爬图片+歌词,命名成和音乐同个格式,然后按名字排序之后就可以用改名软件批量直接改成这个样子了(自以为很机智)
虽然本人对python的理解还停留在曾经因作业要求爬过一次的莎士比亚全集……

具体操作

美汤(程序员都是吃货系列

好像是评论比较难爬
然后没有追求的我就把这一块都去了……
http://www.jianshu.com/p/2b783f7914c6

先对着一个歌单把ID爬下来
然后再对着ID爬各种……(只能想到这样了)

http://www.bkjia.com/Pythonjc/1189598.html

歌曲
https://www.zhihu.com/question/41505181
python 下载mp3(这个例子是百度音乐的)
http://www.th7.cn/Program/Python/201507/499020.shtml

图片
http://www.tuicool.com/articles/6NVjAjr
http://www.th7.cn/Program/Python/201603/775611.shtml
http://www.cnblogs.com/Albert-Lee/p/6276847.html

歌词
https://juejin.im/entry/59185f37da2f60005dee09d3
http://www.cnblogs.com/Beyond-Ricky/p/6757954.html

具体实现

应该可以从如下代码中看到以上一些参考文献的痕迹(捂脸)
虽然拼凑痕迹有点明显orz反正能大概实现功能已经很满意了

读取歌单

# 爬取某歌单的所有歌曲ID
def getSongIdList():songIdList = []playListUrl = 'http://music.163.com/playlist?id=918802417'#空城 finishedsoup = BeautifulSoup(_session.get(playListUrl).content)ul = soup.find('ul', attrs={'class': 'f-hide'})for li in ul.findAll('li'):songId = (li.find('a'))['href'].split('=')[1]print  songIdsongIdList.append(songId)# 去一下重复的歌曲IsongIdList = list(set(songIdList))return songIdList

歌曲名+歌手名

 url = BASE_URL + 'song?id=' + str(song.id)url.decode('utf-8')soup = BeautifulSoup(_session.get(url).content)strArr = soup.title.string.split(' - ')song.singer = strArr[1]name = strArr[0].encode('utf-8')

歌词

def get_lyric_by_music_id(music_id,songstr):#定义一个函数,通过音乐的id得到歌词lrc_url = 'http://music.163.com/api/song/lyric?' + 'id=' + str(music_id) + '&lv=1&kv=1&tv=-1'#lrc_url = BASE_URL + 'song?id=' + str(music_id)lyric=requests.get(lrc_url)json_obj=lyric.text#print(json_obj)j=json.loads(json_obj)file_atr = 'E:\\music\\' + songstr + '.txt'     f = open(file_atr,'wb')f.writelines(songstr.encode('utf8'))f.write('\n')try:#部分歌曲没有歌词,这里引入一个异常lrc=j['lrc']['lyric']pat=re.compile(r'\[.*\]')lrc=re.sub(pat,"",lrc)lrc=lrc.strip()#print type(lrc)f.writelines(lrc.encode('utf8'))f.close()return lrcexcept KeyError as e:f.writelines('No Available Lyric on CloudMusic')f.close()

专辑封面

def get_img(url,songstr):  ##保存图片#print '##正在读取图片##'#print urlurlStream=urllib.urlopen(url)htmlString=urlStream.read()#print htmlStringif( len(htmlString)!=0 ):patternString=r'http://p1.music.126.net/.*?.jpg'searchPattern=re.compile(patternString)imgUrlList=searchPattern.findall(htmlString)imgUrl =imgUrlList[0]#print imgUrlif( len(imgUrl)!= 0 ):urllib.urlretrieve(imgUrl,'E:\\music\\' + songstr + '.jpg')

完整代码

import requests
from selenium import webdriver
from bs4 import BeautifulSoup
import os, json
import base64
import warnings
import re,urllib,uuid
'''
遇到不懂的问题?Python学习交流群:1136201545满足你的需求,资料都已经上传群文件,可以自行下载!
'''warnings.filterwarnings("ignore")
BASE_URL = 'http://music.163.com/'
_session = requests.session()
localPath='d://pythonPath'def createFileWithFileName(localPathParam,fileName):totalPath=localPathParam+'//'+fileNameif not os.path.exists(totalPath):file=open(totalPath,'a+')file.close()class Song(object):def __lt__(self, other):return self.commentCount > other.commentCount# 爬取某歌单的所有歌曲ID
def getSongIdList():songIdList = []playListUrl = 'http://music.163.com/playlist?id=918802417'#空城 finishedsoup = BeautifulSoup(_session.get(playListUrl).content)ul = soup.find('ul', attrs={'class': 'f-hide'})for li in ul.findAll('li'):songId = (li.find('a'))['href'].split('=')[1]print  songIdsongIdList.append(songId)# 去一下重复的歌曲IsongIdList = list(set(songIdList))return songIdList# 获取歌曲信息
def matchSong(songId):song = Song()song.id = songIdreturn songdef get_lyric_by_music_id(music_id,songstr):#定义一个函数,通过音乐的id得到歌词lrc_url = 'http://music.163.com/api/song/lyric?' + 'id=' + str(music_id) + '&lv=1&kv=1&tv=-1'#lrc_url = BASE_URL + 'song?id=' + str(music_id)lyric=requests.get(lrc_url)json_obj=lyric.text#print(json_obj)j=json.loads(json_obj)file_atr = 'E:\\music\\' + songstr + '.txt'f = open(file_atr,'wb')f.writelines(songstr.encode('utf8'))f.write('\n')try:#部分歌曲没有歌词,这里引入一个异常lrc=j['lrc']['lyric']pat=re.compile(r'\[.*\]')lrc=re.sub(pat,"",lrc)lrc=lrc.strip()#print type(lrc)f.writelines(lrc.encode('utf8'))f.close()return lrcexcept KeyError as e:f.writelines('No Available Lyric on CloudMusic')f.close()def get_img(url,songstr):  ##保存图片#print '##正在读取图片##'#print urlurlStream=urllib.urlopen(url)htmlString=urlStream.read()#print htmlStringif( len(htmlString)!=0 ):patternString=r'http://p1.music.126.net/.*?.jpg'searchPattern=re.compile(patternString)imgUrlList=searchPattern.findall(htmlString)imgUrl =imgUrlList[0]#print imgUrlif( len(imgUrl)!= 0 ):urllib.urlretrieve(imgUrl,'E:\\music\\' + songstr + '.jpg')# 设置歌曲的信息
def setSongInfo(song):url = BASE_URL + 'song?id=' + str(song.id)url.decode('utf-8')soup = BeautifulSoup(_session.get(url).content)strArr = soup.title.string.split(' - ')song.singer = strArr[1]name = strArr[0].encode('utf-8')song.name = namesongstr = strArr[0]+ ' - '+ strArr[1]songstr = songstr.replace('/',' ')song.lrc = get_lyric_by_music_id(song.id,songstr)song.img = get_img(url,songstr)# 获取符合条件的歌曲列表
def getSongList():print ' ##正在爬取歌曲编号... ##'songIdList = getSongIdList()print ' ##爬取歌曲编号完成,共计爬取到' + str(len(songIdList)) + '首##'songList=[]for id in songIdList[0:]:song = matchSong(id)if None != song:setSongInfo(song)songList.append(song)print '成功匹配一首{名称:', song.name, ' - ', song.singer,  '}'#  print ' ##爬取完成,符合条件的的共计' + str(len(songList)) + '首##'return songListdef main():songList = getSongList()if __name__ == '__main__':main()

运行过程

[外链图片转存失败(img-47MjNNQ3-1566302376843)(https://upload-images.jianshu.io/upload_images/13406307-a1dec35e4ec7df1b?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

结果

(这里我很怂的把歌手名去掉了因为斜杠的问题解决不掉)
[外链图片转存失败(img-BzCkZ8HE-1566302376843)(https://upload-images.jianshu.io/upload_images/13406307-35414fe7ff93d3aa?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

迷之操作

格式工厂转图片格式 + 拖把更名器改名……
[外链图片转存失败(img-2Ja1G5m3-1566302376843)(https://upload-images.jianshu.io/upload_images/13406307-5d1252d2187dcf6d?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

我想知道这个失败是什么鬼……

[外链图片转存失败(img-2Y2odihY-1566302376844)(https://upload-images.jianshu.io/upload_images/13406307-98f58cef10adbe9c?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)]

python爬取网易云音乐 专辑图片+歌词相关推荐

  1. 【python】 爬取网易云音乐 专辑图片+歌词

    要求 下载一百首歌曲,相关图片以及相关文字信息 存储方式分别为: .mp3 .txt .png 比如第一首歌曲相关信息为001.mp3\001.txt\001.png 觉得像是小朋友的抄写作业有没有- ...

  2. python soup歌词_【python】 爬取网易云音乐 专辑图片+歌词

    要求 下载一百首歌曲,相关图片以及相关文字信息 存储方式分别为: .mp3 .txt .png 比如第一首歌曲相关信息为001.mp3\001.txt\001.png 觉得像是小朋友的抄写作业有没有- ...

  3. python爬音乐-用python爬取网易云音乐歌曲的歌词

    今天我来分享一下如何用python爬取网易云音乐歌曲的歌词,网易云音乐的歌词的爬取思路同前面介绍过的爬取网易云音乐的歌曲评论的爬取思路一致.由于两者的加密思路都是一致的,因此我们只需分析出被加密了的参 ...

  4. python爬取网易云音乐生成王力宏歌曲词云

    python爬取网易云音乐生成王力宏歌曲词云 # -*- coding:utf-8 -*- # 网易云音乐,通过歌手id生成词云 import requests import sys,re,os fr ...

  5. 用Python爬取网易云音乐歌曲

    前天给大家分享了用Python网络爬虫爬取了网易云歌词,在文尾说要爬取网易云歌曲,今天小编带大家一起来利用Python爬取网易云音乐,分分钟将网站上的音乐down到本地. 跟着小编运行过代码的筒子们将 ...

  6. python爬取网易云音乐热评_python爬取网易云音乐评论

    本文实例为大家分享了python爬取网易云音乐评论的具体代码,供大家参考,具体内容如下 import requests import bs4 import json def get_hot_comme ...

  7. python爬取网易云音乐飙升榜音乐_python爬取网易云音乐热歌榜 python爬取网易云音乐热歌榜实例代码...

    想了解python爬取网易云音乐热歌榜实例代码的相关内容吗,FXL在本文为您仔细讲解python爬取网易云音乐热歌榜的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点:python,网易热歌榜 ...

  8. python爬取网易云音乐飙升榜音乐_python爬取网易云音乐热歌榜实例代码

    首先找到要下载的歌曲排行榜的链接,这里用的是: https://music.163.com/discover/toplist?id=3778678 然后更改你要保存的目录,目录要先建立好文件夹,例如我 ...

  9. Python爬取网易云音乐热歌榜(爬虫)

    Python爬取网易云音乐热歌榜歌曲,并下载到本地 找到要下载歌曲排行榜的链接,这里用的是: https://music.163.com/discover/toplist?id=3778678 然后更 ...

最新文章

  1. 简单创建序列和触发器示例
  2. java中的doget_java servlet中doGet()和doPost()方法的用法和区别
  3. python爬虫招聘-Python爬虫实战之(二)| 寻找你的招聘信息
  4. 操作系统 课堂练习题02【8道 经典题目】
  5. Spark _13_topN
  6. django出现 CSRF cookie not set
  7. jdbc mysql user_tab_comments_mysql/jdbc:设置useInformationSchema=true读取表注释信息(table_comment)...
  8. java三年技术差_3年经验Java程序员面阿里P6 差距在哪里
  9. [转]WampServer localhost 图标不显示解决办法
  10. linux登录界面说明,Linux登录界面以及简单使用入门
  11. 【数位dp】模版总结
  12. python pyhook_Python——pyHook监听鼠标键盘事件
  13. Excel 科学计数法数值转换
  14. Android端WebRTC本地音视频采集流程源码分析
  15. python解决百钱百鸡问题
  16. 怎么样在Linux上使用AppImage?
  17. CSS的水平居中、垂直居中和水平垂直居中
  18. 波特兰书呆子晚餐-在那儿和广场
  19. DDR学习心得(一)
  20. OpenFeign中动态URl、动态传递接口地址

热门文章

  1. IPv6进阶:IPv6 过渡技术之 NAT64(IPv6 节点主动访问 IPv4 节点-地址池方式)
  2. java-jar基础应用
  3. 阿里巴巴2015校园招聘面试大礼包
  4. 第29课:AD中class,设计参数,规则的设置
  5. 力扣简单题合集(带答案)
  6. 【CDOJ 1323】柱爷的下凡
  7. 离开腾讯首创业,贾佳亚谈人工智能 2.0 革命,技术究竟该如何变革?
  8. 武汉计算机学校分数线,武汉交通学校2021年招生录取分数线
  9. 不带www的域名强制跳转到www域名,Nginx服务器rewrite重写
  10. python+twilio实现打电话和发短信功能