爬取酷狗音乐Top500榜单
Posted 茶·凯碧
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了爬取酷狗音乐Top500榜单相关的知识,希望对你有一定的参考价值。
import requests
from bs4 import BeautifulSoup
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\'
}
#需求:爬取酷狗TOP500
#需要的字段 排名、歌名、歌曲时长、歌手
def get_info(url):
resp = requests.post(url,headers=headers)
content = resp.content.decode(\'utf-8\')
soup = BeautifulSoup(content,\'lxml\')
rankings = soup.select(\'span.pc_temp_num\') #获取排名
songs = soup.select(\'div.pc_temp_songlist > ul > li> a\') #获取歌名和歌手
times = soup.select(\'span.pc_temp_tips_r > span\') #获取时间
for rank, song, time in zip(rankings, songs, times):
data = {
\'排名\' : rank.get_text().strip(),
\'歌手\' : song.get_text().split(\'-\')[0],
\'歌名\' : song.get_text().split(\'-\')[1],
\'时长\' : time.get_text().strip()
}
print(data)
if __name__ == \'__main__\':
urls = [\'https://www.kugou.com/yy/rank/home/{}-8888.html?from=rank\'.format(str(num)) for num in range(1,24)]
for url in urls:
get_info(url)
以上是关于爬取酷狗音乐Top500榜单的主要内容,如果未能解决你的问题,请参考以下文章