关键词搜索

源码搜索 ×
×

利用python采集数据,让自己的耳朵随时充满美妙声音~

发布2022-04-29浏览988次

详情内容

知识点:

  • 爬虫基本流程
  • requests的使用
  • hashlib的使用

环境]:

  • python 3.8
  • pycharm2021.2

代码

import requests
import time
import hashlib
# 更多资源、源码、解答可加:832157862

headers = {
    'cookie': 'Hm_lvt_d0ad46e4afeacf34cd12de4c9b553aa6=1650954808; cuid=38cef486-7437-d12b-8266-e9f1af79fc91; Hm_lpvt_d0ad46e4afeacf34cd12de4c9b553aa6=1650958214',
    'from': 'web',
    'referer': 'https://music.91q.com/player',
    'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
}
search_timestamp = int(time.time())
word = '薛之谦'
appid = '16073360'
search_r = f"appid={appid}&pageNo=1&pageSize=20&timestamp={search_timestamp}&type=1&word={word}0b50b02fd0d73a9c4c8c3a781c30845f"
search_sign = hashlib.md5(search_r.encode('utf-8')).hexdigest()
search_url = 'https://music.91q.com/v1/search'
search_params = {
    'sign': search_sign,
    'word': word,
    'type': '1',
    'pageNo': '1',
    'pageSize': '20',
    'appid': appid,
    'timestamp': search_timestamp
}
search_resp = requests.get(url=search_url, headers=headers, params=search_params)
music_list = search_resp.json()['data']['typeTrack']
for music in music_list:
    TSID = music['id']
    url = 'https://music.91q.com/v1/song/tracklink'
    timestamp = int(time.time())
    r = f"TSID={TSID}&appid={appid}&timestamp={timestamp}0b50b02fd0d73a9c4c8c3a781c30845f"
    sign = hashlib.md5(r.encode('utf-8')).hexdigest()
    params = {
        'sign': sign,
        'appid': appid,
        'TSID': TSID,
        'timestamp': timestamp
    }
    headers = {
        'cookie': 'Hm_lvt_d0ad46e4afeacf34cd12de4c9b553aa6=1650954808; cuid=38cef486-7437-d12b-8266-e9f1af79fc91; Hm_lpvt_d0ad46e4afeacf34cd12de4c9b553aa6=1650958214',
        'from': 'web',
        'referer': 'https://music.91q.com/player',
        'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36',
    }
    response = requests.get(url=url, params=params, headers=headers)
    music_url = response.json()['data']['path']
    music_name = response.json()['data']['title']
    singer_name = response.json()['data']['artist'][0]['name']
    print(singer_name, music_name, music_url)
    music_data = requests.get(music_url).content
    with open(f'mp3/{music_name}-{singer_name}-{TSID}.mp3', mode='wb') as f:
        f.write(music_data)
# 更多资源、源码、解答可加:832157862
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55

相关技术文章

点击QQ咨询
开通会员
返回顶部
×
微信扫码支付
微信扫码支付
确定支付下载
请使用微信描二维码支付
×

提示信息

×

选择支付方式

  • 微信支付
  • 支付宝付款
确定支付下载