Live2d Test Env

python小实战:哔哩哔哩发送弹幕

爬取一言api,并将获取到的数据发送到哔哩哔哩直播间

import requests
import time
import json
myOnesay = 'https://v1.hitokoto.cn/?c=b'  # 一言开放接口
response = requests.get(myOnesay)
response_json = json.loads(response.text) # 将获取到的文本格式化字典
print(type(response_json) ,response_json['hitokoto'])  # 返回的文本在该key上


def mypas(mymsg=input('请输入弹幕')):
	print(len(mymsg)) # 这里要判断下长度,哔哩哔哩直播间输入的弹幕有字符长度限制(未做)
	data = {
		'bubble': '0',
		'msg': mymsg, # 这里是输入的弹幕
		'color': '16777215',
		'mode': '1',
		'fontsize': '25',
		'rnd': "{}".format(int(time.time())), #即时更新时间 时间戳,不及时更新会被判定登录失效
		'roomid': '12312112', # 这里是哔哩哔哩直播间的房间号
		'csrf': '', # 身份id 
		'csrf_token': '' # 身份id
	}
	headers = {
		'user-agent': "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/111.0.0.0 Safari/537.36 Edg/111.0.1661.41",
		'origin': 'https://live.bilibili.com',
		'cookie': "nostalgia_conf=-1; b_nut=100; b_ut=5; fingerprint3=b967512ee576241cb1bcda1651c3ff77; rpdid=|(u)YJkkYlmk0J'uY~u)Yml)R; _uuid=88FD10E18-6DD9-9FC3-4EA3-CE49D11516CD82674infoc; Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02=1672321161,1672408822,1672494516,1674471463; hit-new-style-dyn=0; hit-dyn-v2=1; CURRENT_FNVAL=4048; fingerprint=074b4c265151c4b30708e9b88abf411f; buvid_fp_plain=undefined; buvid3=C67D19D6-820D-46D4-BAA7-75FB39A31DD1185006infoc; buvid_fp=422a95b38f3e97caf697f472a2732c41; i-wanna-go-back=-1; header_theme_version=CLOSE; home_feed_column=5; bp_video_offset_315488354=769315500042747900; buvid4=8C00FBD9-2D0A-B9AE-19DD-1407507007EA53378-022050214-Y4QrfA6QtX%2F3G6GebvoK0PXVUwj154FQtHcUULr%2BeeHfB7aHbS3q3A%3D%3D; DedeUserID=3493260829920022; DedeUserID__ckMd5=a92a6f6602708c3d; CURRENT_QUALITY=80; bp_video_offset_3493260829920022=undefined; SESSDATA=3acefc28%2C1694171614%2C33db7%2A32; bili_jct=af43dcf5073335020c54c1f5e0f3559c; CURRENT_PID=10cb61d0-c2ef-11ed-b585-9bab9e198992; b_lsid=E858D987_186E5B16F5C; LIVE_BUVID=AUTO9416788908749481; _dfcaptcha=07c918fbec96b6231ab1307a43bb8995; sid=61xaqm5l; PVID=3"
	}
	url = 'https://api.live.bilibili.com/msg/send'
	resp = requests.post(url=url, data=data, headers=headers)
	print(resp.text)
 # 如果有爬取到数据,则自动发送
 response_json['hitokoto']and mypas(response_json['hitokoto'])
 # 这个是做的一个尝试,假设没有爬取到则需要自己手动输入弹幕
 mypas()

以上

posted @ 2023-03-16 00:42  致爱丽丝  阅读(215)  评论(0编辑  收藏  举报