import requests
import re
from bs4 import BeautifulSoup
from tkinter import *
from tkinter import messagebox
print('此程序为获取b站视频封面的Python程序使用Pyinstaller封装')
while True:
try:
while True:
Ua = {
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.150 Safari/537.36'} # Ua 伪装
content = input('输入av或BV号(请务必在开头加上av或BV):')
Reset_content = f'https://www.bilibili.com/video/{content}'
request = requests.get(Reset_content, Ua) # 访问视频主页
soup = BeautifulSoup(request.text, 'lxml') # 煲汤
title = soup.find('title') # 找到title
x = str(title)
rstr = r"[\/\\\:\*\?\"\<\>\|]" # '/ \ : * ? " < > |'
new_title = re.sub(rstr, " ", x) # 替换为下划线
new_new_title = new_title[28:] # 去掉一些没用的字符
new_new_new_title = new_new_title[0:-34] # 去掉没用的字符
image_json = soup.find('meta', itemprop = "image") # 获取视频封面URL
image = image_json.get('content') # 找到具体URL
download = requests.get(image, Ua) # 访问图片地址
with open(f'{new_new_new_title}.jpg', 'wb') as f: # 下载图片
f.write(download.content)
print(f'视频标题为:{new_new_new_title} 封面地址为:{image}, 文件已自动保存到程序工作目录')
except:
print('错误 请检查AV或BV号是否有误')
continue