鱼C论坛

 找回密码
 立即注册
查看: 336|回复: 1

[已解决]程序报错,论坛的大佬的,复制了跑不出来

[复制链接]
发表于 2024-10-11 12:44:36 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
  1. import requests

  2. import re

  3. import json

  4. import os







  5. def get_name(singer):

  6.     url = 'https://c.y.qq.com/soso/fcgi-bin/client_search_cp'

  7.     params = {

  8.     'catZhida': '1',

  9.     'w': singer,

  10.     }

  11.     headers = {

  12.     'referer': 'https://y.qq.com/portal/search.html',

  13.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'

  14.     }

  15.     html = requests.get(url,headers=headers,params=params).text

  16.     content = re.compile('callback\((.*)\)').findall(html)[0]

  17.     content = json.loads(content)

  18.     data = content.get('data')

  19.     song = data.get('song')

  20.     lists = song.get('list')

  21.     name = []

  22.     for list in lists:

  23.         singer = list.get('singer')[0].get('mid')

  24.         name.append(singer)

  25.     name = name[0]

  26.     return name



  27. def get_html(name,singer):

  28.     url = 'https://c.y.qq.com/v8/fcg-bin/fcg_v8_singer_track_cp.fcg'

  29.     params = {

  30.     'singermid': name,

  31.     'order': 'listen',

  32.     'begin': '0',

  33.     'num': '30',

  34.     }

  35.     headers = {

  36.     'referer': 'https://y.qq.com/n/yqq/singer/003aQYLo2x8izP.html',

  37.     'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'

  38.     }

  39.     html = requests.get(url,headers=headers,params=params).text

  40.     return html





  41. def get_music(vkey,songname,filename,singer):

  42.     if vkey and songname:

  43.         url3 = 'http://dl.stream.qqmusic.qq.com/' + filename + '?vkey=' + vkey + '&guid=7133372870&uin=0&fromtag=66'



  44.         headers = {

  45.         'referer': 'https://y.qq.com/n/yqq/singer/003aQYLo2x8izP.html',

  46.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'

  47.         }

  48.         music = requests.get(url3,headers=headers).content

  49.         dir = singer

  50.         if not os.path.exists(dir):

  51.             os.mkdir(dir)

  52.         with open(dir+'/'+songname+'.m4a','wb') as f:

  53.             f.write(music)

  54.         print(songname,'__',singer)



  55. def get_vkey(strMediaMid,songmid,songname,singer):

  56.     if strMediaMid and songmid and songname :

  57.         url2 = 'https://c.y.qq.com/base/fcgi-bin/fcg_music_express_mobile3.fcg'

  58.         params = {

  59.         'g_tk': '5381',

  60.         'jsonpCallback': 'MusicJsonCallback8571665793949388',

  61.         'loginUin': '0',

  62.         'hostUin': '0',

  63.         'format': 'json',

  64.         'inCharset': 'utf8',

  65.         'outCharset': 'utf-8',

  66.         'notice': '0',

  67.         'platform': 'yqq',

  68.         'needNewCode': '0',

  69.         'cid': '205361747',

  70.         'callback': 'MusicJsonCallback8571665793949388',

  71.         'uin': '0',

  72.         'songmid': songmid,

  73.         'filename': 'C400'+ strMediaMid + '.m4a',

  74.         'guid': '7133372870'

  75.         }

  76.         headers = {

  77.         'referer': 'https://y.qq.com/portal/player.html',

  78.         'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.139 Safari/537.36'

  79.         }

  80.         detail_html = requests.get(url2,headers=headers,params=params).text

  81.         vkey_disc = re.compile('MusicJsonCallback8571665793949388\((.*?)\)').findall(detail_html)[0]

  82.         vkey_disc = json.loads(vkey_disc)



  83.         data = vkey_disc['data']

  84.         items = data.get('items')[0]

  85.         vkey = items.get('vkey')

  86.         get_music(vkey,songname,'C400'+ strMediaMid + '.m4a',singer)





  87. def get_list(detail_html,singer):

  88.     if detail_html:

  89.         lists = re.compile('data":{"list":(.*?),"singer_id',re.S).findall(detail_html)[0]

  90.         datas = json.loads(lists)

  91.         for data in datas:

  92.             musicData = data.get('musicData')

  93.             strMediaMid = musicData.get('strMediaMid')

  94.             songmid = musicData.get('songmid')

  95.             songname = musicData.get('songname')

  96.             get_vkey(strMediaMid,songmid,songname,singer)







  97. def main():

  98.     singer = input('请输入您想要下载的歌手:')

  99.     name = get_name(singer)

  100.     detail_html = get_html(name,singer)

  101.     get_list(detail_html,singer)



  102. if __name__ == '__main__':

  103.     main()
复制代码


复制的论坛大神的,跑不出来
报错如下
D:\PYTHON_NEW\pack20240926\pythonProject1\.venv\Scripts\python.exe D:\PYTHON_NEW\pack20240926\pythonProject1\tkinter练习\爬虫测试\测试qq音乐爬虫.py
D:\PYTHON_NEW\pack20240926\pythonProject1\tkinter练习\爬虫测试\测试qq音乐爬虫.py:37: SyntaxWarning: invalid escape sequence '′content=re.compile(′callback\((.∗)
').findall(html)[zxsq-anti-bbcode-0]
D:\PYTHON_NEW\pack20240926\pythonProject1\tkinter练习\爬虫测试\测试qq音乐爬虫.py:177: SyntaxWarning: invalid escape sequence '′vkeydisc=re.compile(′MusicJsonCallback8571665793949388\((.∗?)
').findall(detail_html)[zxsq-anti-bbcode-0]
最佳答案
2024-10-14 18:01:24
爬虫有时效性的, 这报错,一般就是网站请求给的数据不对头,后续清洗数据报错
小甲鱼最新课程 -> https://ilovefishc.com
回复

使用道具 举报

发表于 2024-10-14 18:01:24 | 显示全部楼层    本楼为最佳答案   
爬虫有时效性的, 这报错,一般就是网站请求给的数据不对头,后续清洗数据报错
小甲鱼最新课程 -> https://ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2025-4-19 23:01

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表