Python爬虫获取网易云音乐热门古风歌单并写入excel

Python爬虫获取网易云音乐热门古风歌单并写入excel_第1张图片
image.png

实现:

# -*- coding=utf-8 -*-
import requests
from bs4 import BeautifulSoup
from pyExcelerator import *

# 获取网易云音乐热门古风歌单前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"],
            "cover_url": "http://music.163.com" + item.a["href"],
            "cover_nb": item.select_one(".nb").get_text().strip(),
        }
        cover_list.append(cover)
print len(cover_list)

# 按播放量排序
for i in cover_list:
    if u"万" in i["cover_nb"]:
        i["cover_nb"] = i["cover_nb"].replace(u"万", "0000")
    i["cover_nb"] = int(i["cover_nb"])
cover_list.sort(key=lambda obj: obj.get('cover_nb'), reverse=True)

# 写excel文件
w = Workbook()  # 创建一个工作簿
ws = w.add_sheet(u"网易云音乐热门古风歌单350")  # 创建一个工作表
ws.write(0, 0, u"序号")  # 在1行1列写入
ws.write(0, 1, u"歌单标题")
ws.write(0, 2, u"歌单地址")
ws.write(0, 3, u"歌单播放量")
i = 0
for item in cover_list:
    i += 1
    ws.write(i, 0, i)  # 在i+1行1列写入
    ws.write(i, 1, item["cover_title"])
    ws.write(i, 2, item["cover_url"])
    ws.write(i, 3, item["cover_nb"])
w.save(u"网易云歌单.xlsx")  # 保存
print "写excel完成"

你可能感兴趣的:(Python爬虫获取网易云音乐热门古风歌单并写入excel)