-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path网易音乐.txt
52 lines (46 loc) · 1.62 KB
/
网易音乐.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# -*- coding: UTF-8 -*-
import requests
import pymongo
headers={'Cookie':'appver=1.5.0.75771',
'Referer': 'http://music.163.com/'
}
client = pymongo.MongoClient()
db = client.wangyiyinyue
db_lists = db.playlists
db_lyrics = db.lyrics
def search(id):
url='http://music.163.com/api/search/pc'
data={'s':id,'offset':1,'limit':10,'type':1000}
req=requests.post(url,headers=headers,data=data) #headres和data是必要信息
content=req.json()
lists=[]
for i in content['result']['playlists']: #遍历content['result']['playlists']所有键,包括id键
try:
lists.append(i['id'])
except Exception as e:
print 'None'
continue #用异常处理防止某些不存在id
#print lists
return lists
def list(song_id):
url='http://music.163.com/api/playlist/detail?id={}&updateTime=-1'
for i in song_id:
website=url.format(i)
req=requests.get(website,headers=headers)
content=req.json()
db_lists.update_one({'id':i}, {'$set': content}, upsert=True) #存入数据库
def lyrics():
url = 'http://music.163.com/api/song/lyric?os=pc&id={}&lv=-1&kv=-1&tv=-1'
for playlist in db_lists.find():
for track in playlist['result']['tracks']:
song_id = track['id']
l_url = url.format(song_id)
req = requests.get(l_url, headers=headers)
content = req.json()
db_lyrics.update_one({'id': song_id}, {'$set': content}, upsert=True)
def main():
song_id=search('古风') #得到id,然后通过id得到其他信息
list(song_id) #得到歌单信息
lyrics() #得到歌词
if __name__=='__main__':
main()