Giter Club home page Giter Club logo

bilibili-live-spider's Introduction

Bilibili-Live-Spider

一、声明

1.该项目遵守MIT协议。

2.请勿滥用,本项目仅用于学习和测试!

3.由于使用本项目造成的不良影响和后果与本人无关!

二、实现原理:

1.核心:数据去重

       直播间网页页面可容纳的弹幕和礼物数据是有限的,且这些数据不断地在更新增加,超过一定量后,将会发生滚动覆盖。

       程序每隔一段时间读取一次页面数据,若页面数据没有超出容量,则两次获取的数据在后段会有部分重复。若页面数据已经开始滚动,则两次获取的数据在位置上会发生错位。因此每获取一次数据就需要进行一次比较去重。 去重原理如下图:(在twice数据列表中寻找与once数据列表末端元素相同的元素,扩展once数据列表。)

       一次去重后,once数据列表可以保留作为数据总列表,而twice数据列表可被新的一批数据覆盖,这样就可以实现重复去重

image

2.主要方法

       (1)网页数据定位方法:selenium浏览器对象访问指定直播间url,返回页面源码,再使用xpath定位对应html标签。

       (2)加速去重方法:使用线程池,对弹幕和礼物列表同时去重。

       (3)运行时长控制:分为两种模式,运行指定时长和运行至直播间关闭。

       (4)抓取监控:每进行一次抓取并去重后,使用print输出一次数据列表,以实现对抓取数据的实时监控。

       (5)数据抓取频率:分为两种模式,快模式和慢模式,具体可根据弹幕流量和直播间人数选择。快模式每0.5秒左右读一次页面数据,两类数据最大缓存量都为400条;慢模式每1秒左右读一次页面数据,两类数据最大缓存量都为200条。

       (6)数据存储:当数据缓存量大于最大缓存量写入csv一次,并清空数据缓存,避免大量数据堆积。

三、使用注意事项

1.python环境下直接运行

       (1)模块依赖安装:

pip install selenium
pip install lxml

       (2)下载浏览器驱动(驱动与浏览器版本一定要对应)(浏览器推荐使用Firefox和Chrome):参考 爬虫利器selenium和浏览器驱动安装教程

       (3)修改浏览器驱动路径及配置:

# 如果使用的浏览器是chrome,第97行代码改为:
bro = webdriver.Chrome('你的驱动路径', chrome_options = chrome_options)
# 如果使用的浏览器是firefox,删除4行,改为:
from selenium.webdriver.firefox.options import Options
# 删除94-97行,改为:
ff_options = Options()
ff_options.add_argument('-headless')
bro = webdriver.Firefox('你的驱动路径', firefox_options = ff_options)

       (4)运行提示:程序在发送请求获取数据时可能因网络不稳定而报错,此时等待其快速重连即可,一般不会影响程序运行。

       (5)建议:建议在控制台运行该py源码,因为其清理输出依赖于cmd

2.使用封装的exe应用程序

       realease尚未发布

bilibili-live-spider's People

Contributors

aicorein avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.