当前位置:   article > 正文

Python爬取QQ音乐并下载

python爬取qq音乐代码下载

 参考博客:https://www.w3cschool.cn/article/14374

  1. import requests
  2. import os
  3. import time
  4. import re
  5. import urllib
  6. class Downloader():
  7. id=1
  8. def __init__(self):
  9. self.headers = {
  10. 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.146 Safari/537.36'
  11. }
  12. #根据歌曲名构造完整链接来搜索需要下载的歌曲信息
  13. self.search_url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=txt.yqq.top&searchid=34725291680541638&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=1&n=20&w={}&g_tk=5381&jsonpCallback=MusicJsonCallback703296236531272&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0'
  14. #第一个接口返回的信息来构造完整链接来获取歌曲下载地址的部分信息
  15. self.fcg_url = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg?g_tk=5381&jsonpCallback=MusicJsonCallback9239412173137234&loginUin=0&hostUin=0&format=json&inCharset=utf8&outCharset=utf-8&notice=0&platform=yqq&needNewCode=0&cid=205361747&callback=MusicJsonCallback9239412173137234&uin=0&songmid={}&filename={}.m4a&guid=8208467632'
  16. #歌曲下载地址,需要根据前两个接口获取的信息来构造完整链接
  17. self.downloader_url = 'http://dl.stream.qqmusic.qq.com/{}.m4a?vkey={}&guid=8208467632&uin=0&fromtag=66'
  18. def run(self, keyword, num=1):
  19. # Step1
  20. # 根据歌名搜索,获取所需的信息
  21. print('[INFO]:正在搜索歌曲...')
  22. res = requests.get(self.search_url.format(keyword), headers=self.headers).text
  23. # media_mid--通过正则获取多媒体id
  24. media_mid_temp = re.findall('"media_mid":"(.*?)"', res)
  25. media_mid = []
  26. for i in range(len(media_mid_temp)):
  27. media_mid.append('C400'+media_mid_temp[i])
  28. # songmid--通果正则获取歌曲id
  29. songmid = re.findall('"lyric_hilight":".*?","mid":"(.*?)","mv"', res)
  30. # singer--通过正则获取歌手信息
  31. singer_temp = re.findall('"singer":\[.*?\]', res)
  32. singer = []
  33. for s in singer_temp:
  34. singer.append(re.findall('"name":"(.*?)"', s)[0])
  35. # songname
  36. songname = re.findall('},"name":"(.*?)","newStatus"', res)
  37. # Step2
  38. # 获取下载地址
  39. print('[INFO]:正在解析下载地址...')
  40. urls = []
  41. del_idex = []
  42. songname_keep = []
  43. singer_keep = []
  44. for m in range(len(media_mid)):
  45. try:
  46. fcg_res = requests.get(self.fcg_url.format(songmid[m], media_mid[m]), headers=self.headers)
  47. vkey = re.findall('"vkey":"(.*?)"', fcg_res.text)[0]
  48. urls.append(self.downloader_url.format(media_mid[m], vkey))
  49. songname_keep.append(songname[m])
  50. singer_keep.append(singer[m])
  51. except:
  52. print('[Warning]:One song lost...')
  53. time.sleep(0.5)
  54. print("共找到%d首相关歌曲"%(len(urls)))
  55. for i in range(1,len(urls)+1):
  56. print("%d:%s_%s"%(i,songname_keep[i-1],singer_keep[i-1]))
  57. id=input("请选择要下载的歌曲ID号(备注:数字只能为当期所显示的数字ID):")
  58. while(True):
  59. if id.isdigit():
  60. if int(id)>len(urls):
  61. id=input("输入的整数必须为所显示的ID中的一个:")
  62. else:
  63. print('[INFO]:开始下载歌曲...')
  64. break
  65. else:
  66. id=input("请重新输入,ID必须为整数:")
  67. index=int(id)-1
  68. filepath = './results/{}'.format(songname_keep[index].replace("\\", "").replace("/", "").replace(" ", "")+'_'+singer_keep[index].replace("\\", "").replace("/", "").replace(" ", "")+'.m4a')
  69. urllib.request.urlretrieve(urls[index], filepath)
  70. if __name__ == '__main__':
  71. while True:
  72. print('[INFO]:欢迎使用QQ音乐下载器...')
  73. print('[Author]:腾讯音乐')
  74. keyword = input('请输入歌曲名称:')
  75. print("正在初始化参数:")
  76. dl = Downloader()
  77. dl.run(keyword, songnum)

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/1015759
推荐阅读
相关标签
  

闽ICP备14008679号