实现:
# -*- coding=utf-8 -*-
import json
import requests
from bs4 import BeautifulSoup
# 获取网易云音乐热门古风歌单前10页、歌单地址和播放量
cover_list = []
for i in xrange(0, 10):
params = {
"order": "hot",
"cat": u"古风",
"limit": 35,
"offset": i * 35
}
r = requests.get("http://music.163.com/discover/playlist/", params=params)
soup = BeautifulSoup(r.text, "lxml")
cover_li = soup.select_one("#m-pl-container").select("li")
for item in cover_li:
cover = {
"cover_title": item.a["title"].encode("utf-8"),
"cover_url": "http://music.163.com" + item.a["href"],
"cover_nb": item.select_one(".nb").get_text().strip().encode("utf-8"),
}
cover_list.append(cover)
print len(cover_list) # 350
# 按播放量降序排序
for i in cover_list:
if "万" in i["cover_nb"]:
i["cover_nb"] = i["cover_nb"].replace("万", "0000")
i["cover_nb"] = int(i["cover_nb"])
cover_list.sort(key=lambda obj: obj.get('cover_nb'), reverse=True)
# 写txt文件
with open(u"网易云音乐热门古风歌单350.txt", "a") as f:
f.write(json.dumps(cover_list, indent=2, ensure_ascii=False))
if f:
f.close()
print "写txt完成"