python实现自动发送弹幕

import requests
import random
import time
while True:
    time.sleep(random.randint(5,10))
    lis = ['666','主播真棒','主播很帅','哈哈']
    url = 'https://api.live.bilibili.com/msg/send'
    word = random.choice(lis)
    data = {
        'bubble': '0',
        'msg': word,
        'color': '16777215',
        'mode': '1',
        'room_type': '0',
        'jumpfrom': '77002',
        'reply_mid': '0',
        'reply_attr': '0',
        'replay_dmid': '',
        'fontsize': '25',
        'rnd': '1704603360',
        'roomid': '4421499',
        'csrf': '63844037e95e9286b655e80f442b00de',
        'csrf_token': '63844037e95e9286b655e80f442b00de',
    }
    headers = {
        'Cookie': 'innersign=0; buvid3=147D8190-C7BD-7BA9-1991-11F625BFAD0F63250infoc; b_nut=1704603163; i-wanna-go-back=-1; b_ut=7; b_lsid=E97105AAA_18CE244184E;bsource=search_bing; _uuid=24B48ADE-5F45-F1BA-8F101-92E6EA3C5E9562728infoc; enable_web_push=DISABLE;header_theme_version=undefined; buvid4=54DF23A6-E7BA-5931-0E4C-64707AFE8C9F64640-024010704-8qrN5r0k2p518FoBUY6DBs00hx1F24CGrUVZ2ENL3N3IDPtdKOB3sdEUTsoRlc77; home_feed_column=5; browser_resolution=1536-738; LIVE_BUVID=AUTO7417046031702675;fingerprint=892df77718f2f0b37febc337179c336d; buvid_fp_plain=undefined;SESSDATA=71b8bf90%2C1720155226%2C2dac8%2A11CjAo-NiqsV45WQLIibMGIClQR1VFNwQYnmFTN3Tmtxez29o6Q-1UD-l3AVU8sg-pGGUSVmEtUS1UTnAybzlWWjhVbF9NR01kX1RBQ1RpeE1XVGpMd0ttUl9BQVJmVjJEdzJaWUhPVVRoenF1enZPRlRBZnR0WmlKclV1bGFmbXRURDZyVHl3Mnp3IIEC;bili_jct=63844037e95e9286b655e80f442b00de;DedeUserID=151028355; DedeUserID__ckMd5=c9e195efef0967b0;sid=qheqhqiw; buvid_fp=892df77718f2f0b37febc337179c336d;PVID=6',
        'Origin':'https://live.bilibili.com',
        'Referer':'https://live.bilibili.com/30799587?hotRank=0&session_id=55905b2ccc68f0c235fce6fa00659a2e_D14DA67F-59DA-4E12-9FF9-2CDC71BABCC9&launch_id=1000154&live_from=77002&visit_id=6non4r8jwz00',
        'Sec-Ch-Ua':'"Not_A Brand";v="8", "Chromium";v="120", "Google Chrome";v="120"',
        'Sec-Ch-Ua-Mobile':'?0',
        'Sec-Ch-Ua-Platform':'"Windows"',
        'Sec-Fetch-Dest':'empty',
        'Sec-Fetch-Mode':'cors',
        'Sec-Fetch-Site':'same-site',
        'User-Agent':'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36',
    }
    response = requests.post(url=url,data=data,headers=headers)
    print(response.status_code)

深入学习爬虫后,发现爬虫能做的比我想象得多,包括自动答题脚本,发送弹幕等等。

具体操作,首先进入b站一个直播间,点击network,发送一个弹幕,会发现出现一个send包,对这个包中的url发送请求,post参数,headers请求头都要带完整,其中post参数中的roomid代表直播房间号,msg表示发送的内容

总结:

1.random模块随机选择列表中的元素

lis = ['666','主播真棒','主播很帅','哈哈']

 word = random.choice(lis)

random随机选择1到10中的数字

random.randint(1,10)

2.time模块延时操作

time.sleep(数字)代表延时多少秒操作

你可能感兴趣的:(python,开发语言)