功能
获取网易云音乐个人收藏歌单的相关信息,包括歌单名、歌单地址、歌单封面、播放量等。
将获取的结果保存到本地文件中,以便进行查看。
代码
import time
import csv
import os
from selenium import webdriver
headers = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/68.0.3440.106 Safari/537.36',
}
file_path = r"文件保存路径"
def get_list_info():
url = "网易云音乐个人信息页面地址"
driver = webdriver.Chrome()
driver.get(url)
# 使窗口最大化
driver.maximize_window()
time.sleep(10)
# 进入到iframe框架
driver.switch_to.frame(driver.find_elements_by_tag_name("iframe")[0])
li_list = driver.find_elements_by_xpath('//ul[@id="sBox"]/li')
# 保存歌单信息
res_list = []
for li in li_list:
# 每个歌单信息
list_info = []
# 歌单名
title = li.find_element_by_xpath(".//a[@class='tit f-thide s-fc0']").text
# 歌单url
url = li.find_element_by_xpath(".//a[@class='tit f-thide s-fc0']").get_attribute("href")
# 播放数
plays = li.find_element_by_xpath(".//span[@class='nb']").text
# 歌单封面url
photo_url = li.find_element_by_xpath(".//img").get_attribute("src").split("?")[0]
list_info.append(title)
list_info.append(url)
list_info.append(plays)
list_info.append(photo_url)
res_list.append(list_info)
csv_path = os.path.join(file_path, "wyy_list.csv")
# 保存歌单信息
with open(csv_path, "w", encoding="utf-8", newline='') as sf:
writer = csv.writer(sf)
for l in res_list:
writer.writerow((l[0], l[1], l[2], l[3]))
driver.quit()
if __name__ == '__main__':
# 获取所有歌单信息
get_list_info()