知识点:
- 爬虫基本流程
- requests的使用
- hashlib的使用
环境]:
代码
import requests
import time
import hashlib
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×tamp={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}×tamp={timestamp}0b50b02fd0d73a9c4c8c3a781c30845f"
sign = hashlib.md5(r.encode('utf-8')).hexdigest()
params = {
'sign': sign,
'appid': appid,
'TSID': TSID,
'timestamp': timestamp
}
# 更多资源、源码、解答可加: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',
}
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)
网友评论