该脚本能实现在b站买房。
只需输入cookie(模拟登录)、up主的uid和评论即可。
因为直接登录比较麻烦,本来就是个简易脚本,因此采用cookie登录
获取cookie的方法:
先用浏览器登录一遍b站
再用浏览器打开:https://api.bilibili.com/x/v2/reply/add
然后调出开发者工具,chrome的快捷键是F12,其他浏览器度娘去。
然后刷新一下,找到名为add的,在里面找到Cookie,Cookie的内容就是所需要的。
如图所示:
代码如下:
# -*- coding=utf-8 -*-
import urllib.request
import urllib.parse
import http.cookiejar
import urllib.error
import json
import time
def auto_comment(oid,message,cookie):
headers={
'Accept': 'application/json, text/javascript, */*; q=0.01',
'Accept-Encoding': 'gzip, deflate, br',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Cookie': cookie,
'Host': 'api.bilibili.com',
'Origin': 'https://www.bilibili.com',
'Referer': 'https://www.bilibili.com/video/av'+oid+'/?spm_id_from=333.334.home_popularize.3',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}
url='https://api.bilibili.com/x/v2/reply/add'
comment={
'oid':oid,
'type':'1',
'message':message,
'plat':'1',
'jsonp':'jsonp',
'csrf':'6e3a4a77eb11ba85d2321764935b7bb0'
}
postdata=urllib.parse.urlencode(comment).encode('utf-8')
cj=http.cookiejar.CookieJar()
opener=urllib.request.build_opener(urllib.request.HTTPCookieProcessor(cj))
urllib.request.install_opener(opener)
try:
request=urllib.request.Request(url,headers=headers,data=postdata)
response=opener.open(request)
except urllib.error.URLError as e:
if hasattr(e,'code'):
print(e.code)
if hasattr(e,'reason'):
print(e.reason)
def get_new_video(uid):
url='https://space.bilibili.com/ajax/member/getSubmitVideos?mid='+uid+'&page=1&pagesize=25'
headers={
'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8',
'Accept-Language': 'zh-CN,zh;q=0.9',
'Connection': 'keep-alive',
'Host': 'space.bilibili.com',
'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/66.0.3359.181 Safari/537.36'
}
try:
request=urllib.request.Request(url,headers=headers)
response=urllib.request.urlopen(request)
except urllib.error.URLError as e:
if hasattr(e,'code'):
print(e.code)
if hasattr(e,'reason'):
print(e.reason)
data=response.read()
videodata=json.loads(data)
av_number=videodata['data']['vlist'][0]['aid']
first_video_time=videodata['data']['vlist'][0]['created']
return (av_number,first_video_time)
cookie=input('请输入cookie: ')
uid=input('请输入up空间的uid: ')
comment=input('请输入评论: ')
nowtime=time.time()
while True:
info=get_new_video(uid)
if(info[1]>nowtime):
auto_comment(str(info[0]),comment,cookie)
break
#每隔三分钟试一下
time.sleep(180)
代码托管在:https://github.com/junhao69535/bilibili_comment
欢迎来我个人博客作客:http://120.77.80.230/