栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Python

QQ音乐爬虫

Python 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

QQ音乐爬虫

脑海一直有个想法,想做一个音乐播放的小程序。奈何还只停留在脑海之中。音乐的数据的来源是个需要考虑的问题。之前用Nodejs爬取过酷狗音乐的歌曲,不过酷狗音乐的歌手下的单曲展示有数量上的限制(才有20首)。所以喵上了QQ音乐。QQ音乐和网易云音乐都是比较流行的播放的平台,不过你懂得(小程序)和QQ音乐都是同根生,或许兼容音乐格式会更好。QQ音乐的歌曲的格式是m4a。下面说说爬取音乐URL逻辑。

  • 1.在浏览器的地址栏上面打开下面的地址

    https://y.qq.com/portal/search.html#page=1&searchid=1&remoteplace=txt.yqq.top&t=song&w=%E8%AE%B8%E5%B5%A9
    • 在搜索歌手的时候,最好先用QQ或者微信登录个人账号。QQ音乐可能会限制游客查看更多得到歌手信息。如果登录之后,再打开上面的地址,右键单击审查元素。如下图所示,先勾上红色箭头的勾,刷新,
      然后查看红色框框的请求资源地址:


      审查元素

    • 查看返回的json数据格式,上面的图是许嵩的,下面换了陈奕迅,其实数据格式是一样的,上面红色表示的数据的含义如图所示,其中list每次返回20首歌曲:


      json数据格式

    • 我们获取上面的json的数据格式是为了获取json数据中的mid字段信息,这个字段是获取真实音乐地址的关键数据。mid处于data->song->list[i]->mid,如图所示,每一首歌都有自己的mid字段值:


      mid字段

    • 其中w是搜索的歌手名字的urlencode后的字符串,其他的参数是固定的值。现在w关键参数是许嵩

  • 2.接下来我们要构造获取一个歌手的所有歌曲的mid值:

      https://c.y.qq.com/soso/fcgi-bin/client_search_cp?ct=24&qqmusic_ver=1298&new_json=1&remoteplace=sizer.yqq.song_next&searchid=151387985106450562&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=4&n=20&w=%E9%99%88%E5%A5%95%E8%BF%85&g_tk=162512859&jsonpCallback=searchCallbacksong8555&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0
    :authority:c.y.qq.com
    :method:GET
    :path:/soso/fcgi-bin/client_search_cp?  ct=24&qqmusic_ver=1298&new_json=1&remoteplace=sizer.yqq.song_next&searchid=151387985106450562&t=0&aggr=1&cr=1&catZhida=1&lossless=0&flag_qc=0&p=4&n=20&w=%E9%99%88%E5%A5%95%E8%BF%85&g_tk=162512859&jsonpCallback=searchCallbacksong8555&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0:scheme:https
    accept:**',        'accept-encoding':'gzip, deflate, sdch, br',        'accept-language':'zh-CN,zh;q=0.8',        'cache-control':'no-cache',        'cookie':'pgv_pvi=2539236352; RK=E/NGWlYOOU; pgv_pvid=4700237117; ptui_loginuin=1482816494; ptcz=0ed94d9b03e410a4a4d523a936e1de8f739a265f19407322ac522dc4402dd9f8; pt2gguin=o1482816494; yq_index=0; pgv_si=s1229479936; ts_last=y.qq.com/portal/search.html; ts_uid=7066888440; yqq_stat=0',        'pragma':'no-cache',        'referer':'https://y.qq.com/portal/search.html',        'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
        }
        reqHeader={        ':authority':'c.y.qq.com',        ':method':'GET',        ':path':'/v8/fcg-bin/fcg_play_single_song.fcg?songmid=001bhwUC1gE6ep&tpl=yqq_song_detail&format=jsonp&callback=getOneSongInfoCallback&g_tk=5381&jsonpCallback=getOneSongInfoCallback&loginUin=0&hostUin=0&format=jsonp&inCharset=utf8&outCharset=utf-8¬ice=0&platform=yqq&needNewCode=0',        ':scheme':'https',        'accept':'**',      'accept-encoding':'gzip, deflate, sdch, br',      'accept-language':'zh-CN,zh;q=0.8',      'cache-control':'no-cache',      'cookie':'pgv_pvi=2539236352; RK=E/NGWlYOOU; ptui_loginuin=1482816494; ptcz=0ed94d9b03e410a4a4d523a936e1de8f739a265f19407322ac522dc4402dd9f8; pt2gguin=o1482816494; yq_index=0; tvfe_boss_uuid=6e9af09cbb0ea51c; pgv_pvid=4700237117; o_cookie=1482816494; pgv_si=s7529175040; ts_last=y.qq.com/portal/singer_list.html; ts_uid=7066888440; yqq_stat=0',      'pragma':'no-cache',      'referer':'https://y.qq.com/portal/singer_list.html',      'user-agent':'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36'
      }  def start_requests(self):      yield Request(url=self.url.format(pagenum=1),headers=self.headers,callback=self.parse,dont_filter=True)  def parse(self, response):
          jsonData=self.dealJson(response,["^ GetSingerListCallback(",")$"])
          totalPage=jsonData["data"]["total_page"]      for i in range(totalPage):          yield Request(url=self.url.format(pagenum=i),headers=self.headers,callback=self.insertSinger,dont_filter=True)  def insertSinger(self,response):      # 第一个pattern前面有一个空格
          jsonData=self.dealJson(response,["^ GetSingerListCallback(",")$"])
          singerList=jsonData["data"]["list"]      for i in range(len(singerList)):
              item=singerItem()
              item["singerName"]=singerList[i]["Fsinger_name"]
              item["singer_mid"]=singerList[i]["Fsinger_mid"]          yield item  def dealJson(self,response,pattern):
          text=response.text
          text=re.sub(pattern[0],'',text)
          text=re.sub(pattern[1],'',text)
          jsonData=json.loads(text)      return jsonData
    • 从web地址https://y.qq.com/portal/singer_list.html下面获取歌手信息的请求,如图所示:

      歌手请求头以及请求地址


      歌手数据

    • 获取歌手名字详细代码:

如果获取详细代码可以直接访问 QQMusicCrawler,README有详细的使用说明。



作者:Evtion
链接:https://www.jianshu.com/p/72b4222fadf5


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/221381.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号