Python爬取酷狗TOP100

import time
import requests
from bs4 import BeautifulSoup
headers={
    'User-Agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
}
def get_info(url):
    req=requests.get(url=url,headers=headers)
    soup=BeautifulSoup(req.text)
    ranks=soup.select('.pc_temp_num')
    titles=soup.select('.pc_temp_songlist > ul > li > a')
    times=soup.select('.pc_temp_time')
    for rank,title,time in zip(ranks,titles,times):
        data={
            'rank':rank.get_text().strip(),
            'title':title.get_text().split('-')[1].strip(),
            'signer': title.get_text().split('-')[0].strip(),
            'time':time.get_text().strip()
        }
        print(data)

if __name__=='__main__':
    urls=['http://www.kugou.com/yy/rank/home/{}-8888.html'.format(str(i)) for i in range(1,36)]
    for url in urls:
        get_info(url)
        time.sleep(1)

你可能感兴趣的:(Python爬取酷狗TOP100)