python实现爬取哔哩哔哩视频代码并保存到本地文件夹D:\2233中
时间: 2024-05-01 19:18:40 浏览: 239
以下是Python代码实现爬取哔哩哔哩视频并保存到本地文件夹D:\2233中的操作步骤:
```python
import requests
from contextlib import closing
# 视频链接地址
url = 'https://upos-sz-mirrorcos.bilivideo.com/upgcxcode/09/12/232461209/232461209-1-16.mp4?e=ig8euxZM2rNcNbhjhwbNv8BqBWNvNbhK38W7MgugEnNcXBB_&deadline=1613735034&gen=playurl&nbs=1&oi=3071608497&os=cosbv&platform=html5&trid=cd29dfeb625e482ba9e9d8c973a3dc3du&uipk=5&upsig=8c7c15eef6b5ac9d44f0f558be0ea29f'
# 本地文件路径及文件名
local_path = 'D:/2233/Python.mp4'
with closing(requests.get(url, stream=True)) as response:
chunk_size = 1024 # 单次请求最大值
content_size = int(response.headers['content-length']) # 内容体总大小
if response.status_code == 200: # 判断是否响应成功
print('文件大小:%0.2f MB' % (content_size / chunk_size / 1024)) # 输出文件大小
with open(local_path, 'wb') as file:
for data in response.iter_content(chunk_size=chunk_size):
file.write(data)
print('下载中:%.2f%%' % (file.tell() / content_size * 100), end='\r')
else:
print('链接异常')
```
如此实现,便可完成对指定视频的爬取并保存到本地指定路径的操作。
阅读全文