由于想学习视频, 一直很中意哔哩哔哩
个人习惯每看个视频都要做笔记嘛, 所以想获取一下视频的目录信息
之前有查到一个获取视频目录的方式
是在控制台进行操作的
但是嘛, 现在控制台不让复制了, 没办法, 只好写一个小功能了
今天是 2023/11/14 15:15
功能代码如下:
import requests
import json
import os
from datetime import datetime
from bs4 import BeautifulSoup
# 参考链接:
# https://zhuanlan.zhihu.com/p/117569614
# https://blog.csdn.net/weixin_42914706/article/details/129112667
def print_directory(data):
soup = BeautifulSoup(data, 'html.parser')
# print(bs)
target = 'window.__INITIAL_STATE__='
script_tags = soup.find_all('script')
count = 1
for script in script_tags:
if target in script.text:
data = script.text.replace(target, "").split('};')[0] + '}'
data = json.loads(data)
for index in data.get('videoData').get('pages'):
print('P{0} {1}'.format(count, index.get('part')))
count += 1
break
if __name__ == '__main__':
current_time = datetime.now().strftime("%Y%m%d%H%M%S")
file_name = 'Bilibili视频目录_{0}.txt'.format(current_time)
if not os.path.exists(file_name):
with open(file_name, 'w') as file:
print("文件创建成功")
file_size = os.path.getsize(file_name)
if file_size == 0:
url = input("请输入网址:")
print("网址: ", url)
print("访问网址获取数据")
res = requests.get(url)
if os.path.exists(file_name):
os.remove(file_name)
with open(file_name, 'w', encoding='utf-8') as f:
f.write(res.text)
print_directory(res.text)
else:
print("不用访问网址")
with open(file_name, 'r', encoding='utf-8') as f:
read = f.read()
print_directory(read)