马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
为什么只能爬到10首歌呢?歌单里不止10首 #!/user/bin/env python
# -*- coding:utf-8 -*-
# time:2021/6/17
#需求:下载网易云歌单
import requests as req
from lxml import etree
import os
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.93 Safari/537.36"
}
url = "https://music.163.com/playlist?id=6775871260"
# url = "https://music.163.com/playlist?id=6731975194"
page_text = req.get(url=url,headers=headers).text
tree = etree.HTML(page_text)
id_list = tree.xpath('//*[@id="song-list-pre-cache"]//a/@href')
name_list = tree.xpath('//*[@id="song-list-pre-cache"]//a/text()')
i = 0
print(name_list,id_list)
#为毛只有10首??
if not os.path.exists("./wangyi list music"):
os.mkdir("./wangyi list music")
try:
for id_ in id_list:
id_ = id_.split('=')[-1]
name = name_list[i]
i +=1
base_url = "http://music.163.com/song/media/outer/url?id=%s.mp3" % id_
content = req.get(url=base_url,headers=headers).content
with open("./wangyi list music/"+name+".mp3",'wb') as fp:
fp.write(content)
print(name+".mp3下载完成!")
except FileNotFoundError or OSError:
print(name+"因出错没有下载")
|