酷狗TOP500歌曲榜网页如图:
通过观察链接和审查元素,使用BeautifulSoup即可轻松爬取,直接贴代码:
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/49.0.2623.112 Safari/537.36'
}
def get_info(url):
web_data = requests.get(url,headers=headers)
soup = BeautifulSoup(web_data.text,'lxml')
ranks = soup.select('span.pc_temp_num')
titles = soup.select('#rankWrap > div.pc_temp_songlist > ul > li > a')
times = soup.select('span.pc_temp_tips_r > span')
for rank,title,time in zip(ranks,titles,times):
data = {
'rank':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 = ['http://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)
结果如下:
{'rank': '1', 'singer': '刘子璇 ', 'song': ' 嘴巴嘟嘟', 'time': '4:08'}
{'rank': '2', 'singer': 'Olly Murs ', 'song': ' That Girl', 'time': '2:57'}
{'rank': '3', 'singer': '曾惜 ', 'song': ' 讲真的', 'time': '3:59'}
{'rank': '4', 'singer': '杨坤、郭采洁 ', 'song': ' 答案', 'time': '3:51'}
{'rank': '5', 'singer': '小潘潘、小峰峰 ', 'song': ' 学猫叫', 'time': '3:29'}
{'rank': '6', 'singer': '夏磊 ', 'song': ' 可能否', 'time': '3:49'}
{'rank': '7', 'singer': '莉哥 ', 'song': ' 让我做你的眼睛', 'time': '1:46'}
{'rank': '8', 'singer': '烟把儿乐队 ', 'song': ' 纸短情长 (完整版)', 'time': '2:53'}
{'rank': '9', 'singer': '庄心妍 ', 'song': ' 再见只是陌生人', 'time': '4:12'}
{'rank': '10', 'singer': '孙语赛、萧全 ', 'song': ' 不仅仅是喜欢', 'time': '3:48'}
。。。。。。