# code at 2021-10-1
# 根据html标签获取数据
# 需要安装包pip lxml
import requests
import bs4
import ctypes
import tkinter
import tkinter.ttk
import tkinter.scrolledtext
import pyperclip
import lxml
headers = {
# 'authority': 'developer.mozilla.org',
# 'pragma': 'no-cache',
# 'cache-control': 'no-cache',
# 'upgrade-insecure-requests': '1',
'user-agent':'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/75.0.3770.100 '
'YaBrowser/19.7.0.1635 Yowser/2.5 Safari/537.36',
# 'accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,
# application/signed-exchange;v=b3', 'accept-encoding': 'gzip, deflate, br', 'accept-language': 'zh-CN,
# zh-TW;q=0.9,zh;q=0.8,en-US;q=0.7,en;q=0.6', 'cookie': 你的cookie,
}
global view
global dm
global time
global like
global coin
global collect
global share
def get_video_info():
global view
global dm
global time
global like
global coin
global collect
global share
half_url = GetBVString.get()
if 'http' in half_url:
bv_string = half_url.strip().split('?')[0].split('/')[4]
GetBVString.delete(0, '')
GetBVString.insert(0, bv_string)
else:
bv_string = half_url
response = requests.get(f'https://www.bilibili.com/video/{bv_string}', headers=headers)
soup = bs4.BeautifulSoup(response.text, "lxml")
view = soup.select('#viewbox_report > div > span.view')[0].text.replace('播放 · ', '')
dm = soup.select('#viewbox_report > div > span.dm')[0].text.replace('弹幕', '')
like = soup.select('#arc_toolbar_report > div.ops > span.like')[0].text.replace('\n ', '')
coin = soup.select('#arc_toolbar_report > div.ops > span.coin')[0].text.replace('\n ', '').replace('\n ',
'')
collect = soup.select('#arc_toolbar_report > div.ops > span.collect')[0].text.replace('\n ', '')
share = soup.select('#arc_toolbar_report > div.ops > span.share')[0].text.replace('\n ', '')
time = soup.select('#viewbox_report > div > span:nth-child(3)')[0].text
viewLabel.configure(text=view)
dmLabel.configure(text=dm)
likeLabel.configure(text=like)
coinLabel.configure(text=coin)
collectLabel.configure(text=collect)
shareLabel.configure(text=share)
timeLabel.configure(text=time)
def paste():
GetBVString.delete(0, '')
GetBVString.insert(0, pyperclip.paste())
def clear():
GetBVString.delete(0, '')
window = tkinter.Tk()
ctypes.windll.shcore.SetProcessDpiAwareness(1)
ScaleFactor = ctypes.windll.shcore.GetScaleFactorForDevice(0)
window.tk.call('tk', 'scaling', ScaleFactor /72)
window.title('视频数据监控')
GetBVString = tkinter.ttk.Entry(window)# , width=50
confirm = tkinter.ttk.Button(window, text="获取", command=get_video_info)
paste = tkinter.ttk.Button(window, text="粘贴", command=paste)
clear = tkinter.ttk.Button(window, text="清空", command=clear)
tkinter.ttk.Label()
viewLabelHint = tkinter.ttk.Label(window, text='播放:')
dmLabelHint = tkinter.ttk.Label(window, text='弹幕:')
timeLabelHint = tkinter.ttk.Label(window, text='时间:')
likeLabelHint = tkinter.ttk.Label(window, text='点赞:')
coinLabelHint = tkinter.ttk.Label(window, text='投币:')
collectLabelHint = tkinter.ttk.Label(window, text='收藏:')
shareLabelHint = tkinter.ttk.Label(window, text='分享:')
viewLabel = tkinter.ttk.Label(window, text='')
dmLabel = tkinter.ttk.Label(window, text='')
likeLabel = tkinter.ttk.Label(window, text='')
coinLabel = tkinter.ttk.Label(window, text='')
collectLabel = tkinter.ttk.Label(window, text='')
shareLabel = tkinter.ttk.Label(window, text='')
timeLabel = tkinter.ttk.Label(window, text='')
GetBVString.grid(column=0, row=0, columnspan=2)
confirm.grid(column=2, row=0)
paste.grid(column=3, row=0)
clear.grid(column=4, row=0)
viewLabelHint.grid(column=0, row=1, sticky='E')
dmLabelHint.grid(column=0, row=2, sticky='E')
likeLabelHint.grid(column=0, row=3, sticky='E')
coinLabelHint.grid(column=0, row=4, sticky='E')
collectLabelHint.grid(column=0, row=5, sticky='E')
shareLabelHint.grid(column=0, row=6, sticky='E')
timeLabelHint.grid(column=0, row=7, sticky='E')
viewLabel.grid(column=1, row=1, sticky='W')
dmLabel.grid(column=1, row=2, sticky='W')
likeLabel.grid(column=1, row=3, sticky='W')
coinLabel.grid(column=1, row=4, sticky='W')
collectLabel.grid(column=1, row=5, sticky='W')
shareLabel.grid(column=1, row=6, sticky='W')
timeLabel.grid(column=1, row=7, sticky='W')
window.mainloop()