B站视频爬取
import jsonimport os.path
import re
import subprocess
import requests
headers = {
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) '
'Chrome/98.0.4758.102 Safari/537.36 Edg/98.0.1108.62',
'referer': 'https://www.bilibili.com/'
}
def main(url):
# 获取页面html代码
resp = requests.get(url=url, headers=headers)
html_code = resp.text
# 解析标题
title = re.findall('<h1 id="video-title" title="(.*?)" class="video-title">', html_code)
# 解析播放地址
play_info = re.findall('<script>window.__playinfo__=(.*?)</script>', html_code)
print(title)
print(play_info)
json_data = json.loads(play_info)
# 获取音频地址
audio_url = json_data['data']['dash']['audio']['baseUrl']
print(audio_url)
# 获取视频地址默认最清晰
video_url = json_data['data']['dash']['video']['baseUrl']
print(video_url)
audio_resp = requests.get(url=audio_url, headers=headers, stream=True)
if not os.path.exists('movie'):
os.mkdir('movie')
with open(f'movie/{title}.mp3', mode='wb') as f:
for audio_data in audio_resp.iter_content(10240):
f.write(audio_data)
video_resp = requests.get(url=video_url, headers=headers, stream=True)
with open(f'movie/{title}.mp4', mode='wb') as f:
for video_data in video_resp.iter_content(10240):
f.write(video_data)
command = f'D:/JetBrains/ffmpeg-5.0.1/ffmpeg-5.0.1-essentials_build/bin/ffmpeg -i movie/{title}.mp4 -i movie/{title}.mp3 -c:v copy -c:a aac -strict experimental movie/{title}-merger.mp4'
print(command)
subprocess.run(command, shell=True)
if __name__ == '__main__':
main('https://www.bilibili.com/video/BV1LY4y187ii') vicky526356 发表于 2022-5-31 21:34
看代码就晕乎乎的,不如成品好看
不好意思,不擅长UI 代码有了挺好,但是能不能发个成品,想运行个程序还要下载个pycharm,python
然后下载 json os.path re subprocess requests 这几个包 ,然后复制代码再运行 ,才能看到成品......
对新手不太友好,就比如说我玩了4年吾爱只是因为找资源比较方便 成品呢?希望发个成品出来。 看代码就晕乎乎的,不如成品好看 MPFENG 发表于 2022-5-31 21:19
成品呢?希望发个成品出来。
不好意思,不擅长UI 思路最重要,ui不是很简单 但是这个代码下载下来只有音频呀 chaselove 发表于 2022-5-31 21:52
但是这个代码下载下来只有音频呀
都有得,安装ffpmeg 改下代码改成您的安装目录,会合并音频和视频 有成品的话对于我们这些小白来说就太好了{:1_918:}