当前位置: 首页 > 后端技术 > Python

python 虾咪停服了...用python爬取虾咪最近播放的1000首歌

时间:2023-03-26 11:20:22 Python

Python虾米已经停止服务了……用python爬取虾米最近播放的1000首歌曲,真是让人不爽。虽然当你去一个新的应用时,让他们的算法熟悉你的喜好并不是很难,但我还是习惯了虾米的界面。虾米现在可以支持自己的播放列表、收藏等全方位导出,但毕竟是用了这么久的听歌app,这么久的播放记录没法导出到其他app,而虾米小其独特的听音乐氛围和环境是其他听歌软件无法比拟的,更何况虾米音乐的推荐算法也很受欢迎。太难受了……赶紧把最新的音乐收听记录保存起来封存起来。虾米只打开了最后1000条记录,应该是数据库只存了这么多吧。。。毕竟是被阿里巴巴遗弃的孩子。个人设置里的选项:勾选后,听歌记录会公开,可以被程序抓取。Python代码============话不多说,直接上传代码:python==3.6.2urllib==1.26.2lxml==4.6.2pandas版本可选#-*-coding:utf-8-*-"""创建于2021/1/513:46@author:Irvinfaith@email:Irvinfaith@hotmail.com"""fromurllib.requestimporturlopenfromlxmlimportetreeimportpandasaspd#definexpathsong_name_xpath='//div[@class="singers-nameem"]/a/text()'singer_xpath='//div[@class="singersCOMPACT"]/a[1]/text()'album_xpath='//div[@class="album"]/a[1]/text()'duration_xpath='//span[@class="duration"]/text()'#定义爬取总页数total_page=34defcrawl_recent(user_id):song_name_list=[]singer_list=[]album_list=[]duration_list=[]for_inrange(total_page):print(_)url=f"https://www.xiami.com/list?page={_}&query=%7B%22userId%22%3A%22{user_id}%22%7D&scene=record&type=song"page=urlopen(url).read().decode("utf-8",'ignore')parse=etree.HTML(page)for_song_name,_singer,_album,_durationinzip(parse.xpath(song_name_xpath)、parse.xpath(singer_xpath)、parse.xpath(album_xpath)、parse.xpath(duration_xpath)):song_name_list.append(str(_song_name))singer_list.append(str(_singer))album_list.append(str(_album)))duration_list.append(str(_duration))recent_music=pd.DataFrame({"songname":song_name_list,"singer":singer_list,"album":album_list,"duration":duration_list})returnrecent_musicif__name__=='__main__':#输入虾米用户idrecent_music=crawl_recent("12345678")#recent_music.to_csv("D:/xiami_recent_1000.csv",index=0,encoding='utf-8')recent_music.to_excel("D:/xiami_recent_1000.xlsx")输入虾米的用户id,执行会输出最新的歌曲。输出结果:综上,以上就是用python爬取虾米播放记录的代码过程。可以关注私信我~