天天看点

基于python获取网易云热门歌单及封面

先看一下图片背后的代码

基于python获取网易云热门歌单及封面

非常好,里面歌单名字,id,以及封面图片都有了.在复制url的时候注意把/#删掉,要不然爬不了.

import requests
from lxml import etree
url='https://music.163.com/discover/playlist'
head={
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/85.0.4183.83 Safari/537.36'
}
respone=requests.get(url,headers=head)
html=etree.HTML(respone.text)
id_list=html.xpath('//a[contains(@href,"playlist?") and contains(@class,"msk")]/@title')
pic=html.xpath('//img[contains(@src,"http://p2.music.126.net")]/@src')
temp=dict(zip(id_list,pic))
print(temp)
           

如果不限定class的范围会找到两份歌单,会重复

基于python获取网易云热门歌单及封面