python爬取酷狗音乐Top500榜单
程序员文章站
2022-06-15 14:17:55
目录网页情况python 代码运行效果总结网页情况爬取数据包含歌曲排名、歌手、歌曲名、歌曲时长python 代码import requests #请求网页获取网页数据 from bs4 import...
网页情况
爬取数据包含
歌曲排名、歌手、歌曲名、歌曲时长
python 代码
import requests #请求网页获取网页数据 from bs4 import beautifulsoup #解析网页数据 import time #时间库 #user-agent,伪装成浏览器,便于爬虫的稳定性 headers = { "user-agent": "mozilla/5.0 (windows nt 10.0; win64; x64) applewebkit/537.36 (khtml, like gecko) chrome/97.0.4692.71 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('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().replace("\n","").replace("\t","").split('-')[1], "song":title.get_text().replace("\n","").replace("\t","").split('-')[0], "time":time.get_text().strip() } print(data) if __name__ == '__main__': urls = ["https://www.kugou.com/yy/rank/home/{}-8888.html".format(str(i)) for i in range(1,24)] for url in urls: get_info(url) time.sleep(1)
运行效果
总结
到此这篇关于python爬取酷狗音乐top500榜单的文章就介绍到这了,更多相关python爬取酷狗音乐榜单内容请搜索以前的文章或继续浏览下面的相关文章希望大家以后多多支持!