爬取酷狗音乐Top500列表【《从零开始学python网络爬虫》】

	```
	#coding:utf-8
	import requests
	from bs4 import BeautifulSoup
	import time
	
	headers = {
	"User-Agent":"Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.132 Safari/537.36 QIHU 360SE"
	}
	def get_info(url):
	    wb_data = requests.get(url,headers=headers)
	    soup = BeautifulSoup(wb_data.text,'lxml')
	    ranks = soup.select('#rankWrap > div.pc_temp_songlist > ul > li:nth-child(3) > span.pc_temp_num')
	    titles = soup.select("#rankWrap > div.pc_temp_songlist > ul > li:nth-child(3) > a")
	    times = soup.select("#rankWrap > div.pc_temp_songlist > ul > li:nth-child(3) > span.pc_temp_tips_r > span")
	    for rank,title,time in zip(ranks,titles,times):
	        data = {
	            'ranks':rank.get_text().strip(),
	            'singer':title.get_text().split('-')[0],
	            'song':title.get_text().split('-')[1],
	            'time':time.get_text().strip()
	        }
	        print data
	
	if __name__ == "__main__":
	    urls = ['https://www.kugou.com/yy/rank/home/{}-8888.html?from=rank'.format(str(i)) for i in range(1,24)]
	    for url in urls:
	        get_info(url)
	    time.sleep(1)
	```

你可能感兴趣的:(python)