谷歌浏览器:
import requests import time cookie={'Cookie':'l=v; LIVE_BUVID=AUTO9115296677131525; Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02=1529667823,1529678970,1529719880; finger=7b4f413b; buvid3=8933D0B8-6007-4CE8-86BA-C93029CCD26416479infoc; sid=luecjskq; fts=1529669115; DedeUserID=311817936; DedeUserID__ckMd5=cca4969724abe2bb; SESSDATA=03bf401f%2C1532261056%2C82174446; bili_jct=5f9e891cfd70c2c6a703fd494bf69dc7; Hm_lpvt_8a6e55dbd2870f0f5bc9194cddf32a02=1529722625; _dfcaptcha=0691ef63ed3afa8e000cc79433fa186b; UM_distinctid=1642a729295c8-0408a57676b7d6-143e7140-fa000-1642a7292961'} sendurl='https://api.live.bilibili.com/msg/send' sendform={ 'color':'16777215', 'csrf_token':'5f9e891cfd70c2c6a703fd494bf69dc7', 'fontsize':'25', 'mode':'1', 'msg':'hello you', 'rnd':'1529669186', 'roomid':'1355059' } r=requests.post(sendurl,data=sendform,cookies=cookie) headers = { 'Host': 'api.live.bilibili.com', 'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:43.0) Gecko/20100101 Firefox/43.0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8', 'Accept-Language': 'zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3', 'Accept-Encoding': 'gzip, deflate', 'Referer': 'http://www.baidu.com', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', } url='https://api.live.bilibili.com/AppRoom/msg?room_id=1355059' resp=requests.get(url,headers=headers) data=resp.json() print(data) print(type(data)) for i in data['data']['room']: print(i['text'])
火狐浏览器:
import time import random import requests class danMu(object): def __init__(self,roomid): self.roomid=roomid self.cookie={'Cookie':'l=v; LIVE_BUVID=AUTO9115296677131525; Hm_lvt_8a6e55dbd2870f0f5bc9194cddf32a02=1529667823,1529678970,1529719880; finger=7b4f413b; buvid3=8933D0B8-6007-4CE8-86BA-C93029CCD26416479infoc; sid=luecjskq; fts=1529669115; DedeUserID=311817936; DedeUserID__ckMd5=cca4969724abe2bb; SESSDATA=03bf401f%2C1532261056%2C82174446; bili_jct=5f9e891cfd70c2c6a703fd494bf69dc7; Hm_lpvt_8a6e55dbd2870f0f5bc9194cddf32a02=1529722625; _dfcaptcha=0691ef63ed3afa8e000cc79433fa186b; UM_distinctid=1642a729295c8-0408a57676b7d6-143e7140-fa000-1642a7292961'} self.geturl='https://api.live.bilibili.com/ajax/msg' #提交的参数 self.getform={ 'csrf_token':'5f9e891cfd70c2c6a703fd494bf69dc7', 'data_behavior_id':'127508b9ff3e630', 'data_source_id':'system', 'roomid':self.roomid, 'visit_id':'6u82bcskv1ts' } self.sendurl='https://api.live.bilibili.com/msg/send' self.sendform = { 'color': '16777215', 'csrf_token': '5f9e891cfd70c2c6a703fd494bf69dc7', 'fontsize': '25', 'mode': '1', 'msg': '烦', 'rnd': '1529669186', 'roomid': self.roomid } def getDanMu(self): self.gethtml=requests.post(self.geturl,self.getform) self.danMuList=list(map(lambda i:self.gethtml.json()['data']['room'][i]['text'],range(len(self.gethtml.json()['data']['room'])))) self.mese=self.danMuList[random.randrange(1,10)] print(len(self.gethtml.json()['data']['room'])) for i in self.danMuList: print(i) print(self.gethtml) print(self.gethtml.json()) print(type(self.gethtml.json())) def sendDanMu(self): self.sendhtml=requests.post(self.sendurl,data=self.sendform,cookies=self.cookie) print(self.sendhtml.json()) if __name__=='__main__': #实例化 danmu = danMu('1355059').sendDanMu() danmu=danMu('1355059').getDanMu()