涡轮蒸鸭 发表于 2022-6-1 07:41

B站视频爬取

import json
import 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')

larryjjd 发表于 2022-6-1 07:41

谢谢@Thanks!

倌木x 发表于 2022-6-1 07:48

感谢楼主分享

金先生 发表于 2022-6-1 08:30

谢谢分享!

hyfe 发表于 2022-6-1 08:46

多谢楼主分享

wang521127 发表于 2022-6-1 08:46

谢谢大佬

123hiha 发表于 2022-6-1 12:38

顶帖是一种态度,也是一种尊重。
页: [1]
查看完整版本: B站视频爬取