鱼C论坛

 找回密码
 立即注册
查看: 3717|回复: 37

[技术交流] 闲来无事写了一个爬bilibili的代码,看自己学了多少时长

[复制链接]
发表于 2021-1-7 04:13:24 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
本帖最后由 Daniel_Zhang 于 2021-1-8 21:27 编辑

模块在此:

文件名 get_data_set.py
注意修改一下 LOCATION 变成自己的文件夹
  1. import pickle, os
  2. import easygui as g
  3. import get_total_time as data

  4. LOCATION = os.getcwd() + '/html_css_js_flask/learning_progress_count/'

  5. url = input('enter the url address\n')

  6. my_time = []
  7. my_time = data.get_durations(url)

  8. # make a new binary file to store the data in list my_time
  9. def add_data_set(my_time):
  10.     # auto create a new file if it not exists, or write into the file directly if the file exists
  11.     pickle_file = open(LOCATION + 'html_learn_progress.testing','wb')   # wb is write binary, do not mind the file name, it can be anything
  12.     pickle.dump(my_time,pickle_file)    # dump the list into the file
  13.     pickle_file.close()

  14. def data_set_read():
  15.     if __name__ != '__main__':
  16.         add_data_set(my_time)
  17.     pickle_file = open(LOCATION + 'html_learn_progress.testing','rb')   # rb is read binary
  18.     my_list2 = pickle.load(pickle_file) # load the binary data
  19.     if __name__ == '__main__':
  20.         print(my_list2) # show the data
  21.     length_data_set = len(my_list2)
  22.     g.msgbox(msg = 'data set insert successful :)' + '\n\n' + 'total insert: ' + str(length_data_set),title='System Warning',ok_button='Get it !')
  23.     return my_list2

  24. if __name__ == '__main__':
  25.     add_data_set(my_time)
  26.     data_set_read()
复制代码


又是一个模块,爬虫模块,自动获取数据,文件名: get_total_time.py
  1. import re, ssl
  2. import requests

  3. def open_url(url):
  4. # encoding: utf-8

  5.     headers = {
  6.     'User-Agent':'Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36',
  7.     'Accept': 'text/html',
  8.     'Cookie': "_uuid=1DBA4F96-2E63-8488-DC25-B8623EFF40E773841infoc; buvid3=FE0D3174-E871-4A3E-877C-A4ED86E20523155831infoc; LIVE_BUVID=AUTO8515670521735348; sid=l765gx48; DedeUserID=33717177; DedeUserID__ckMd5=be4de02fd64f0e56; SESSDATA=cf65a5e0%2C1569644183%2Cc4de7381; bili_jct=1e8cdbb5755b4ecd0346761a121650f5; CURRENT_FNVAL=16; stardustvideo=1; rpdid=|(umY))|ukl~0J'ulY~uJm)kJ; UM_distinctid=16ce0e51cf0abc-02da63c2df0b4b-5373e62-1fa400-16ce0e51cf18d8; stardustpgcv=0606; im_notify_type_33717177=0; finger=b3372c5f; CURRENT_QUALITY=112; bp_t_offset_33717177=300203628285382610"
  9.    
  10.     }
  11.     f = open('testing_new.txt','w')
  12.     ssl._create_default_https_context = ssl._create_unverified_context
  13.     html = requests.get(url,headers=headers).text # 获取url内容
  14.     f.write(html) # 写入 url内容到文件,决定如何写下面的正则表达式
  15.     f.close()
  16.     return html

  17. def get_durations(url):
  18.     html = open_url(url)
  19.     m = r'"cid":.+?,'
  20.     match = str(re.findall(m, html)[0])
  21.     match = match.split(':')[-1]
  22.     match = match.split(',')[0] # 获得第一个视频的 cid 用来辅助获取完整的播放列表
  23.     p = r'\[{"cid":' + match + '.+?]'
  24.     pic = re.findall(p, html)  # 获取完整的播放列表
  25.     final_result = []
  26.     q = r'"duration":.+?,'
  27.     pic = str(pic)
  28.     duration = re.findall(q, pic)   # 获取每一个视频的播放时长的列表(此处包含了 class 名称,需要进一步进行处理)
  29.     duration = str(duration)
  30.     y = r':.+?,'
  31.     time_get = re.findall(y, duration)  # 获得每一个视频的播放时长列表(进一步进行处理)

  32.     for each in range(len(time_get)):   # 清除所有不必要的内容
  33.         time_get[each] = time_get[each].split(':')[-1]
  34.         time_get[each] = time_get[each].split(',')[0]
  35.         temp = time_get[each]
  36.         final_result.append([int(temp) // 60, int(temp) % 60])  # 将时间转换成 分钟:秒
  37.     return final_result

  38. if __name__ == '__main__':
  39.     url = input('enter\n')
  40.     #open_url(url)
  41.     get_durations(url)
复制代码


主程序在此,文件名 time_adding.py
  1. import pickle
  2. import easygui as g
  3. import get_data_set as data_set

  4. sum_second = 0  # time already used, initial be zero



  5. # calculate the progress
  6. def calculate(my_list2):
  7.     global sum_second
  8.     total_time = 0
  9.     for each in range(len(my_list2)):
  10.         total_time += 60 * int(my_list2[each][0]) + int(my_list2[each][1])
  11.         if each < already_take:
  12.             sum_second += 60 * int(my_list2[each][0]) + int(my_list2[each][1])
  13.     string1 = "hours already take: " + str(sum_second/(60*60)) +' / ' + str(total_time/(60*60))
  14.     string2 = "current percentage: " + str((sum_second/total_time) * 100) + ' %'
  15.     #g.msgbox(msg = string1 + '\n\n' + string2,title='System Warning',ok_button='Get it !')
  16.     g.msgbox(msg = string1 + '\n\n' + string2,title='System Warning',ok_button='Get it !')


  17. if __name__ == "__main__":
  18.     already_take = input('how many unit you already take up to now?\n')
  19.     try:
  20.         already_take = int(already_take)
  21.     except ValueError:
  22.         g.msgbox(msg = 'seems enter is wrong, please check it and enter again!', title='System Warning',ok_button='Get it !')
  23.         exit()
  24.     my_list2 = data_set.data_set_read()
  25.     calculate(my_list2)
复制代码


p.s 三个文件请放在同一个文件夹下面
只需要输入指定的 bilibili 视频链接,含有 BV号 或者 a v 号的那个链接,以及自己学完了多少个章节(学完了第一讲就输入1,以此类推)
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-7 04:15:35 | 显示全部楼层
另外发现一个挺有意思的api,可以用BV号和cid获取需要的东西,文章链接如下:

https://www.bilibili.com/read/cv5245087
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-7 08:50:56 | 显示全部楼层
向楼主学习啊,我才刚开始学,不知道什么时候可以有这么厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-7 10:51:19 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-7 11:21:15 | 显示全部楼层

回帖奖励 +1 鱼币

学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-7 11:53:50 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-7 13:19:59 | 显示全部楼层

回帖奖励 +1 鱼币

学习一下
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-7 16:51:12 | 显示全部楼层
刚学习,希望向楼主一样厉害
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-7 17:14:26 | 显示全部楼层
提高了一点中奖概率,没有获得鱼币的可以多尝试几次呀
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-7 18:34:46 | 显示全部楼层

回帖奖励 +1 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-7 22:12:46 | 显示全部楼层

回帖奖励 +1 鱼币

刚学习,好多看不懂
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-7 22:16:05 | 显示全部楼层
luckyboyzzz 发表于 2021-1-7 22:12
刚学习,好多看不懂

加油,等你认真学完了小甲鱼的课程,相信你一定能看懂的~我也才刚学完没多久
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2021-1-8 21:27:35 | 显示全部楼层
别沉啊,还有币呢,改了爆率了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-8 22:08:58 | 显示全部楼层

回帖奖励 +2 鱼币

学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

 楼主| 发表于 2021-1-10 03:46:50 | 显示全部楼层
别沉啊,还有鱼币
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-10 09:54:09 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-10 17:59:47 | 显示全部楼层

回帖奖励 +2 鱼币

捞一手
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-10 21:19:22 | 显示全部楼层

回帖奖励 +2 鱼币

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2021-1-10 23:01:06 | 显示全部楼层

回帖奖励 +2 鱼币

只学会了print
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2021-1-13 20:20:04 | 显示全部楼层

回帖奖励 +2 鱼币

学习
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-4-28 12:44

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表