Chinaunix首页 | 论坛 | 博客

分类: IT业界

2021-01-08 11:30:26

知识点:
1.request库的使用
2.re正则表达式
3.json字符串
4.搜索歌曲爬取对应的音频文件

简介:
我这边使用的是QQ音乐的网址爬取的音频

1.首先选择网址
2.选择后F12查看headers的请求头和 接口对应的关系
3.配置body体
4.循环的方式下载mp3
5.读写拿到的mp3

具体代码如下:

点击(此处)折叠或打开

  1. #-*- coding: UTF-8 -*-


  2. import requests
  3. import re



  4. headers = {
  5.     "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.132 Safari/537.36",
  6. }
  7. keyword = input('请输入你想下载的歌曲名:')
  8. url = ''
  9. params = {
  10.     "ct": " 24",
  11.     "qqmusic_ver": " 1298",
  12.     "new_json": " 1",
  13.     "remoteplace": " txt.yqq.song",
  14.     "searchid": " 66595602585102401",
  15.     "t": " 0",
  16.     "aggr": " 1",
  17.     "cr": " 1",
  18.     "catZhida": " 1",
  19.     "lossless": " 0",
  20.     "flag_qc": " 0",
  21.     "p": " 1",
  22.     "n": " 10",
  23.     "w": keyword,
  24.     "g_tk_new_20200303": " 1390029147",
  25.     "g_tk": " 1390029147",
  26.     "loginUin": " 1164153961",
  27.     "hostUin": " 0",
  28.     "format": " json",
  29.     "inCharset": " utf8",
  30.     "outCharset": " utf-8",
  31.     "notice": " 0",
  32.     "platform": " yqq.json",
  33.     "needNewCode": " 0"
  34. }
  35. response = requests.get(url, params=params, headers=headers)
  36. search_result = eval(re.findall('callback\((.*)\)', response.text)[0])
  37. song_info = search_result['data']['song']['list']
  38. songname = []
  39. songmid = []
  40. singer = []
  41. for i in song_info:
  42.     songname.append(i['songname'])
  43.     songmid.append(i['songmid'])
  44.     singer.append(i['singer'][0]['name'])
  45. tplt = "{0:<10}\t{1:<10}\t{2:<20}"
  46. print(tplt.format("序号", "歌手", "歌名"))
  47. for i in range(len(songname)):
  48.     print(tplt.format(i, singer[i], songname[i]))
  49. songid = songmid[int(input('请输入序号选择你要下载的歌曲:'))]
  50. url1 = 'https://u.y.qq.com/cgi-bin/musicu.fcg'
  51. params1 = {
  52.     "-": "getplaysongvkey8636402145203543",
  53.     "g_tk": "1390029147",
  54.     "loginUin": "1164153961",
  55.     "hostUin": "0",
  56.     "format": "json",
  57.     "inCharset": "utf8",
  58.     "outCharset": "utf-8",
  59.     "notice": "0",
  60.     "platform": "yqq.json",
  61.     "needNewCode": "0",
  62.     "data": '{"req":{"module":"CDN.SrfCdnDispatchServer","method":"GetCdnDispatch","param":{"guid":"7469768631","calltype":0,"userip":""}},"req_0":{"module":"vkey.GetVkeyServer","method":"CgiGetVkey","param":{"guid":"7469768631","songmid":["' + songid + '"],"songtype":[0],"uin":"1164153961","loginflag":1,"platform":"20"}},"comm":{"uin":1164153961,"format":"json","ct":24,"cv":0}}'
  63. }
  64. response = requests.get(url1, params=params1, headers=headers)
  65. songlink = response.json()['req_0']['data']['midurlinfo'][0]['purl']
  66. if songlink == '':
  67.     print('这首歌是付费歌曲无法下载。')
  68. else:
  69.     with open(keyword + '.mp3', mode='wb') as f:
  70.         f.write(requests.get('' + songlink, headers=headers).content)
  71.         print('歌曲下载成功')

阅读(1288) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~