├── Pic ├── find_cookie.png ├── logo.jpg ├── logo.png └── run.gif ├── README.md ├── Spiders └── bilibili_live_barrage.py └── requirements.txt /Pic/find_cookie.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Henryhaohao/Bilibili_Live_Barrage/660ab9240e23645fe274dafddd3330d448d2efe1/Pic/find_cookie.png -------------------------------------------------------------------------------- /Pic/logo.jpg: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Henryhaohao/Bilibili_Live_Barrage/660ab9240e23645fe274dafddd3330d448d2efe1/Pic/logo.jpg -------------------------------------------------------------------------------- /Pic/logo.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Henryhaohao/Bilibili_Live_Barrage/660ab9240e23645fe274dafddd3330d448d2efe1/Pic/logo.png -------------------------------------------------------------------------------- /Pic/run.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/Henryhaohao/Bilibili_Live_Barrage/660ab9240e23645fe274dafddd3330d448d2efe1/Pic/run.gif -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | 实时监控B站直播弹幕并发送跟随弹幕 ![enter image description here](Pic/logo.png) 2 | =========================== 3 | ![](https://img.shields.io/badge/Python-3.6.3-green.svg) ![](https://img.shields.io/badge/requests-2.18.4-green.svg) 4 | ### Bilibili直播官网 - https://live.bilibili.com/ 5 | 6 | |Author|:sunglasses:Henryhaohao:sunglasses:| 7 | |---|--- 8 | |Email|:hearts:1073064953@qq.com:hearts: 9 | 10 | 11 | **** 12 | ## :dolphin:声明 13 | ### 软件均仅用于学习交流,请勿用于任何商业用途!感谢大家! 14 | ## :dolphin:介绍 15 | ### 该项目为Python实时爬取[B站直播](https://live.bilibili.com/)弹幕并发送跟随弹幕 16 | - 项目介绍:通过传入用户登录B站后得到的Cookie(可以登录后抓包获取),即可实现监控B站直播弹幕并发送跟随弹幕 17 | - 运行方式:运行Spiders目录下bilibili_live_barrage.py文件即可 18 | ## :dolphin:运行环境 19 | Version: Python3 20 | ## :dolphin:安装依赖库 21 | ``` 22 | pip3 install -r requirements.txt 23 | ``` 24 | ## :dolphin:运行截图 25 | > - **如何获取登录后的Cookie值**

26 | ![enter image description here](Pic/find_cookie.png) 27 | 28 | > - **运行过程**

29 | ![enter image description here](Pic/run.gif) 30 | ## :dolphin:**总结** 31 | > **最后,如果你觉得这个项目不错或者对你有帮助,给个Star呗,也算是对我学习路上的一种鼓励!
32 | 哈哈哈,感谢大家!笔芯~**:cupid::cupid: 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /Spiders/bilibili_live_barrage.py: -------------------------------------------------------------------------------- 1 | # !/user/bin/env python 2 | # -*- coding:utf-8 -*- 3 | # time: 2018/2/2--21:19 4 | __author__ = 'Henry' 5 | 6 | ''' 7 | 爬取B站直播弹幕并发送跟随弹幕 8 | ''' 9 | 10 | import requests, re 11 | import time 12 | import random 13 | 14 | 15 | def main(): 16 | print('*' * 30 + '欢迎来到B站直播弹幕小助手' + '*' * 30) 17 | url = input('请输出您要查看的直播房间网址链接:') 18 | cookie = input('请输入账号登录后的Cookie值:') 19 | token = re.search(r'bili_jct=(.*?);', cookie).group(1) 20 | # 获取roomid 21 | html = requests.get(url).text 22 | if re.search(r'room_id":(.*?),', html): 23 | roomid = re.search(r'room_id":(.*?),', html).group(1) 24 | print('直播房间号为:' + roomid) 25 | else: 26 | print('抱歉,未找到此直播房间号~') 27 | while True: 28 | # 爬取: 29 | url = 'https://api.live.bilibili.com/ajax/msg' 30 | form = { 31 | 'roomid': roomid, 32 | 'visit_id': '', 33 | 'csrf_token': token # csrf_token就是cookie中的bili_jct字段;且有效期是7天!!! 34 | } 35 | headers = { 36 | 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/55.0.2883.87 Safari/537.36', 37 | 'Cookie': cookie 38 | } 39 | html = requests.post(url, data=form) 40 | result = html.json()['data']['room'] 41 | for i in result: 42 | print('[' + i['nickname'] + ']:' + i['text']) 43 | 44 | barrage = result[random.randint(0, len(result) - 1)]['text'] + '666' # 随机取出第7,8,9条弹幕中的任一条来发送 45 | # 跟随发送: 46 | url_send = 'https://api.live.bilibili.com/msg/send' 47 | data = { 48 | 'color': '16777215', 49 | 'fontsize': '25', 50 | 'mode': '1', 51 | 'msg': barrage, 52 | 'rnd': int(time.time()), 53 | 'roomid': roomid, 54 | 'csrf_token': token 55 | } 56 | try: 57 | html_send = requests.post(url_send, data=data, headers=headers) 58 | result = html_send.json() 59 | if result['msg'] == '你被禁言啦': 60 | print('*' * 30 + '您被禁言啦!!! 跟随弹幕发送失败~' + '*' * 30) 61 | exit() 62 | if result['code'] == 0 and result['msg'] == '': 63 | print('*' * 30 + '[' + barrage + ']' + ' 跟随弹幕发送成功~' + '*' * 30) 64 | else: 65 | print('*' * 30 + '[' + barrage + ']' + ' 跟随弹幕发送失败' + '*' * 30) 66 | except: 67 | print('*' * 30 + '[' + barrage + ']' + ' 跟随弹幕发送失败' + '*' * 30) 68 | time.sleep(1) 69 | 70 | 71 | if __name__ == '__main__': 72 | main() 73 | -------------------------------------------------------------------------------- /requirements.txt: -------------------------------------------------------------------------------- 1 | requests==2.18.4 2 | --------------------------------------------------------------------------------