草莓干 发表于 2020-12-30 20:29:52

用Python获取B站播放历史记录

<p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">本文来源于:乐聚机器人王松博士《用Python获取B站播放历史记录》</span></p><p><br/></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">最近 B 站出了一个年度报告,统计用户一年当中在 B 站上观看视频的总时长和总个数。过去一年我居然在 B 站上看了 2600+ 个视频,总计 251 个小时,居然花了这么多时间,吓得我差点把 Bilibili App 卸载了...</span></p><p style="text-align: center;"></p><p><br/></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">然而我又很好奇,到底我在 B 站上都看了些什么类型 小姐姐 的视频,用几行 Python 代码实现了一下。</span></p><p><br/></p><p><br/></p><section style="border-bottom: 1px solid #ddd;margin: 0 auto 10px;"><p class="135brush" data-brushtype="text" style="padding: 0px 5px 6px; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: rgb(239, 112, 96); display: inline-block; margin: 0px 0px -1px; font-weight: normal; line-height: 1.1; font-size: 18px;"><strong><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 18px;">获取请求 Api 接口与 Cookie</span></strong></p></section><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">实现起来非常容易,获取 cookie 模拟请求即可</span></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">1. 使用 chrome 浏览器</span></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">2. 登陆 B 站,进入历史记录 https://www.bilibili.com/account/history</span></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">3. 在网页任意位置,鼠标右键 检查</span></p><p style="text-align: center;"></p><p><br/></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">4. 按照下图所示,进入 Network 页面,筛选框输入 history,对结果进行筛选,页面滚轮往下即可看到浏览过程中的历史记录请求的 Header</span></p><p style="text-align: center;"></p><p><br/></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">5. 将 Header 下, cookie 一行的字符串复制出来到一个 cookie.txt 文本里</span></p><p style="text-align: center;"></p><p><br/></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;"><br/></span></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;"></span></p><section style="border-bottom: 1px solid #ddd;margin: 0 auto 10px;"><p class="135brush" data-brushtype="text" style="padding: 0px 5px 6px; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: rgb(239, 112, 96); display: inline-block; margin: 0px 0px -1px; font-weight: normal; line-height: 1.1; font-size: 18px;"><strong><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 18px;">Python 代码实现</span></strong></p></section><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">伪造浏览器请求</span><br/><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;"></span></p><p style="text-align: center;"></p><p style="text-align: center;"></p><p><br/></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">使用 cookie 模拟请求</span></p><p style="text-align: center;"></p><p><br/></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">代码非常简单,完整代码在 https://github.com/wangshub/bilibili-history</span></p><p><br/></p><p><br/></p><section style="border-bottom: 1px solid #ddd;margin: 0 auto 10px;"><p class="135brush" data-brushtype="text" style="padding: 0px 5px 6px; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: rgb(239, 112, 96); display: inline-block; margin: 0px 0px -1px; font-weight: normal; line-height: 1.1; font-size: 18px;"><strong><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 18px;">存在的问题</span></strong></p></section><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">本来想拿到所有的播放记录,做一些统计和预测,但是经过实测,B 站只能获取到最近 1000 条或者最近 3 个月的播放记录</span></p><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">如果想获得更多,只能做一个监测程序,不停地从接口获取数据</span></p><p><br/></p><p><br/></p><section style="border-bottom: 1px solid #ddd;margin: 0 auto 10px;"><p class="135brush" data-brushtype="text" style="padding: 0px 5px 6px; border-bottom-width: 2px; border-bottom-style: solid; border-bottom-color: rgb(239, 112, 96); display: inline-block; margin: 0px 0px -1px; font-weight: normal; line-height: 1.1; font-size: 18px;"><strong><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 18px;">安全问题</span></strong></p></section><p style="line-height: 1.75em; margin-bottom: 15px;"><span style="font-family: 微软雅黑, Microsoft YaHei; font-size: 14px;">尽量不要使用不安全的 wifi 网络,有可能会被别有用心之人获取网络请求的Package,易泄露个人隐私。</span><br/></p><p><br/></p><link rel="stylesheet" href="//bbs.lejurobot.com/source/plugin/wcn_editor/public/wcn_editor_fit.css?v134_kKx" id="wcn_editor_css"/>
页: [1]
查看完整版本: 用Python获取B站播放历史记录