Last active
May 13, 2022 11:27
-
-
Save ksqsf/0296e40caadbdcd895cfc70c941b598c to your computer and use it in GitHub Desktop.
下载 Bilibili 音频(编号以 au 开头的投稿)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import requests | |
# | |
# *** 修改这个 *** | |
# | |
audio_id = '832257' | |
# 完事后会下载到一个 m4a 文件和一个 cover.jpg | |
s = requests.Session() | |
s.headers.update({'Referer': 'https://www.bilibili.com/'}) | |
cover_url = s.get('https://www.bilibili.com/audio/music-service-c/web/song/info?sid=' + audio_id).json()['data']['cover'] | |
r = s.get(cover_url, stream=True) | |
open('cover.jpg', 'wb').write(r.raw.read()) | |
# 注:file_url 带一个 ID,是一次性的,第二次请求或不带 ID 会 403。 | |
# User-Agent 也必须是一个合理的,否则也会 403。 | |
file_url = s.get('https://www.bilibili.com/audio/music-service-c/web/url?sid=' + audio_id).json()['data']['cdns'][0] | |
s.headers.update({ | |
'User-Agent': 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:99.0) Gecko/20100101 Firefox/99.0', | |
}) | |
r = s.get(file_url, stream=True) | |
open(f'au{audio_id}.m4a', 'wb').write(r.raw.read()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment