python爬取酷狗排行音乐

Posted on 2019-12-19 12:04  zhuyu123  阅读(240)  评论(0编辑  收藏  举报
import time
import requests
from bs4 import BeautifulSoup
urlFormat='https://www.kugou.com/yy/rank/home/{}-8888.html'
count=1
rank=1
while 1:
    url=urlFormat.format(count)
    res=requests.get(url)
    soup=BeautifulSoup(res.text, 'html.parser')
    songName=[s.text.strip() for s in soup.select('a.pc_temp_songname')]
    songTime=[s.text.strip() for s in soup.select('span.pc_temp_time')]
    if songName and songTime:
        for sName,sTime in zip(songName,songTime):
            print('{: <4}{}{}'.format(rank,sName,sTime))
            rank+=1
        count+=1
        time.sleep(1)
    else:
        break

Copyright © 2024 zhuyu123
Powered by .NET 9.0 on Kubernetes