当前位置:网站首页>视、音频分开的网站内容如何合并?批量下载代码又该如何编写?
视、音频分开的网站内容如何合并?批量下载代码又该如何编写?
2022-07-23 05:37:00 【茜茜是帅哥】
前言
嗨喽,大家好呀~这里是爱看美女的茜茜呐
今天我们要采集的网站呢,是国内知名的视频弹幕网站~
这里有及时的动漫新番,活跃的ACG氛围,有创意的Up主,还有不知多沙雕的网友
而它作为我们本次的目的,它也是非常有意思的,因为它下载的时候是视、音频分开的~
现在,话不多说,让我们开始叭

本篇文章目录(可自由点击你想看的地方)
本片文章代码提供者: 青灯教育-自游
准备工作
下面的尽量跟我保持一致哦~不然有可能会发生报错
环境:
- Python 3.8
- Pycharm 2021.2版本
- ffmpeg <需要设置环境变量>
软件作用:合成视频和音频
模块:
- import requests >>> pip install requests
内置模块 你安装好python环境就可以了
- import re
- import json
- import subprocess

思路流程: <通用>
一. 数据来源分析
- 确定我们需求 采集什么网站上面的什么数据内容
- 分析我们想要shipin数据以及shipin标题内容 -->> 可以请求那个网址得到相应数据内容
shipin内容, 是可以在网页源代码有的 搜索playinfo就有相关数据… 通过开发者工具抓包分析
二. 代码实现步骤
- 发送请求, 对于shipin播放页面url地址发送请求
- 获取数据, 获取网页源代码 <有很多数据内容>
- 解析数据, 提取我们想要数据内容
- 保存数据, 可以把shipin内容保存本地文件夹

代码
网址里的网名被我删啦,你可以看一下它的链接如何的然后自己添加一下
如果你实在不会或有点点小懒癌的小可耐也可以私聊我领取完整源码哦~
导入模块
import os
# 导入数据请求模块 <使用请求工具去发送请求>
import requests
# 导入正则模块
import re
# 导入json
import json
# 导入格式化输出模块
import pprint
# 导入进程模块
import subprocess
import threading
1. 发送请求
对于shipin播放页面url地址发送请求
ef get_video_content(bv_id):
url = f'https://www..com/video/{
bv_id}' # 确定网址, 确定自己要网址是那个
# headers请求头, 用来伪装python代码, 防止被服务器识别出来是你爬虫程序.... user-agent 用户代理 表示浏览器基本身份标识
# 通过requests模块里面get请求方法<函数>对于url地址发送请求... 等号左边的都是自定义变量 不能以数字开头 不建议使用关键词命名
# 有颜色url是函数参数 关键字传参
response = requests.get(url=url, headers=headers) # <Response [200]> 响应对象 200状态码表示请求成功
print(response)
2. 获取数据
获取网页源代码 <有很多数据内容>
# print(response.text) # response.text 获取响应对象文本数据 >>> 字符串数据
3. 解析网站
提取我们想要数据内容 recssxpath 建议都要学习, 选择最适合用的
re.findall(‘“title”:“(.*?)”,’, response.text)
使用re模块里面findall<找到所有>方法 从什么地方去找什么数据
从response.text 里面去找 "title":"(.*?)", 其中 (.*?) 这段是我们想要数据
正则匹配提取出来数据, 返回列表数据 [0] 根据索引位置取值, 在python里面索引位置是从0开始计数

title = re.findall('"title":"(.*?)",', response.text)[0]
# 前端小知识点 前端标签两个两个一起 response.text 字符串类型
html_data = re.findall('<script>window.__playinfo__=(.*?)</script>', response.text)[0] # 获取视频信息 也是复制
# print(html_data)
# print(type(html_data)) # <class 'str'> type() 查看数据类型
json_data = json.loads(html_data) # 转数据类型
# print(json_data)
# print(type(json_data)) # <class 'dict'> 字典数据类型, 字典取值 不会5 会6 根据冒号左边的内容, 提取冒号右边内容
# pprint.pprint(json_data)
# print(json_data['code'])
audio_url = json_data['data']['dash']['audio'][0]['baseUrl']
video_url = json_data['data']['dash']['video'][0]['baseUrl']
print(audio_url)
print(video_url)
print(title)
4. 保存数据
图片 视频 音频 特定格式文件 都获取二进制数据进行保存
# audio_content = requests.get(url=audio_url, headers=headers).content # response.content 获取二进制数据内容
# video_content = requests.get(url=video_url, headers=headers).content # response.content 获取二进制数据内容
# with open('video\\' + title + '.mp3', mode='wb') as f:
# f.write(audio_content)
# with open('video\\' + title + '.mp4', mode='wb') as f:
# f.write(video_content)
#
# cmd = f"ffmpeg -i video\\{title}.mp4 -i video\\{title}.mp3 -c:v copy -c:a aac -strict experimental video\\{title}output.mp4"
# subprocess.run(cmd, shell=True)
# os.remove(f'video\\{title}.mp4')
# os.remove(f'video\\{title}.mp3')
5. 翻页爬取
for page in range(1, 13):
print(f'正在采集第{
page}页数据内容')
link = f'https://api..com/x/space/arc/search?mid=1305006386&ps=30&tid=0&pn={
page}&keyword=&order=pubdate&jsonp=jsonp'
headers = {
'Referer': 'https://www..com/',
'user-agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/101.0.0.0 Safari/537.36'
}
""" response.text 获取文本 response.content 获取二进制 response.json() 获取json字典数据 m3u8 """
resp_json = requests.get(url=link, headers=headers).json()
for index in resp_json['data']['list']['vlist']:
bv_id = index['bvid']
print(bv_id)
尾语
感谢你观看我的文章呐~本次航班到这里就结束啦
希望本篇文章有对你带来帮助 ,有学习到一点知识~
躲起来的星星也在努力发光,你也要努力加油(让我们一起努力叭)。
最后,博主要一下你们的三连呀(点赞、评论、收藏),不要钱的还是可以搞一搞的嘛~
不知道评论啥的,即使扣个6666也是对博主的鼓舞吖 感谢

边栏推荐
- 【ROS进阶篇】第八讲 URDF文件的语法详解
- 简述redis特点及其应用场景
- Matlab中的滤波器
- 达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程
- Two strategies for building AI products / businesses (by Andrew ng)
- C1 -- vivado configuration vs code text editor environment 2022-07-21
- "The six programming languages I want most!"
- LearnOpenGL - Introduction
- Notifier Nordic fire engine power supply maintenance and daily maintenance
- R语言使用DALEX包对h2o包构建的机器学习模型进行解释分析:总结及实战
猜你喜欢

【ROS进阶篇】第八讲 URDF文件的语法详解

C语言n番战--结构体(七)

Powerbi Getting Started Guide

PMP practice once a day | don't get lost in the exam -7.22

Deploy storageclass trample record

NOTIFIER诺帝菲尔消防主机电源维修及日常维护

Visual studio 2022 interesting and powerful intelligent auxiliary coding
![[visual slam] orb slam: tracking and mapping recognizable features](/img/bb/d6a99bb1bff6bbe1f781e567cc1176.png)
[visual slam] orb slam: tracking and mapping recognizable features
![[ROS advanced chapter] Lesson 8 syntax explanation of URDF file](/img/ad/038d088d5cd17784d3e2d7291bb750.jpg)
[ROS advanced chapter] Lesson 8 syntax explanation of URDF file

Murata power maintenance switch server power maintenance and main functional features
随机推荐
8、曲面几何
3DMAX first skin brush weights, then attach merge
[swift bug] Xcode prompt error running playground: failed to prepare for communication with playground
遇到了ANR错误
Redis source code and design analysis -- 6. Compressed list
Cadence学习之路(八)PCB放置元器件
Alibaba cloud object storage service OSS front and rear joint debugging
H1 -- HDMI interface test application 2022-07-15
达人专栏 | 还不会用 Apache Dolphinscheduler?大佬用时一个月写出的最全入门教程
Concepts and differences of bit, bit, byte and word
Redis source code and design analysis -- 8. Object system
7. Texture mapping
52832Dongle的安装
H1--HDMI接口测试应用2022-07-15
C#的partial用法
Basic concepts of software testing
比特,比特,字節,字的概念與區別
A case study on the collaborative management of medical enterprise suppliers, hospitals, patients and other parties by building a low code platform
Why does MySQL index use b+ tree?
C ivalueconverter interface usage example