这个比数码之家登陆难多了。。。
用到一个第三方模块rsa,自己记得装,语言是Python,最近写的东西都是Python的,Python是世界上最好的语言
还有另一个模块,名字是CookieBrows.py
coding:utf-8
**import **re
**import **urllib
**import **urllib2
**import **cookielib
**class **CookieBrowser(object):
构造方法,用来传递初值
**def **__init__(self):
self.cookie=cookielib.MozillaCookieJar()
self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
**def **saveCookie(self,filename):
self.cookie.save(filename,ignore_expires=True,ignore_discard=True)
**def **loadCookie(self,filename):
self.cookie.load(filename,ignore_expires=True,ignore_discard=True)
**def **getCookies(self):
**return **self.cookie
**def **setCookies(self,cookie):
self.cookie=cookie
self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
**def **resetCookies(self):
self.cookie=cookielib.CookieJar()
self.opener=urllib2.build_opener(urllib2.HTTPCookieProcessor(self.cookie))
**def **open(self,addr,param):
**return **self.opener.open(addr,param)
**def **read(self,addr):
**return **self.opener.open(addr).read()
**def **post(self,url,**param):
postdata=urllib.urlencode(param)
**return **self.opener.open(url,postdata)
**def **rawpost(self,url,param):
**return **self.opener.open(url,param)
**def **hasStr(self,url,parrent):
**return **re.findall(parrent,self.read(url))
自己写的模块,这样方便存cookie,也不用每次写一个东西都要写请求头什么的
下面是登陆代码
coding:utf-8
**import **ssl
ssl._create_default_https_context = ssl._create_unverified_context
**import **time
**import **re
**import **rsa
**import **json
**import **urllib
**import **binascii
**import **random
**from **CookieBrowser **import **CookieBrowser
**class **Bilibili():
**def **init(self,username,password):
self.username = username
self.password = password
self.brow = CookieBrowser()
try:
self.brow.loadCookie(self.username)
except:
**pass
**** **self.key = self.getkey()
获取验证码
**def ****getVcode**(self,filename='vcode.jpg'):
f=open(filename,‘wb’)
f.write(self.brow.read(‘https://passport.bilibili.com/captcha’))
f.close()
测试是否登录
**def ****testLogin**(self):
try:
**return **‘的个人空间’ **in **re.findall(’\w\W]*?’,self.brow.read(‘http://space.bilibili.com’))[0][7:-8]
**except **:
**return **False
# 获取登录需要的一些信息
**def **getLoginInfo(self):
**return **json.loads(self.brow.read(‘https://passport.bilibili.com/login?act=getkey’))
加密密码
**def ****encryptpwd**(self,passwd, hash):
password = hash’hash’] + passwd
pub_key = hash’key’]
pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(pub_key)
message = rsa.encrypt(str(password), pub_key)
message = binascii.b2a_base64(message)
**return **message
获取某没卵用判断是否登录的key
**def ****getkey**(self):
**return **json.loads(self.brow.read(‘https://passport.bilibili.com/qrcode/getLoginUrl’))‘data’]'oauthKey’]
登录
**def ****login**(self,vcode):
password=self.encryptpwd(self.password,self.getLoginInfo())
**print **password
postdata=urllib.urlencode({
‘act’:‘login’,
‘gourl’:‘https://passport.bilibili.com/login’,
‘keeptime’:‘2592000’,
‘pwd’:password,
‘userid’:self.username,
‘vdcode’:vcode.upper()
})
**print **postdata
try:
self.brow.rawpost(‘https://passport.bilibili.com/login/dologin’,postdata)
**return **self.testLogin()
except:
**return **self.testLogin()
finally:
self.brow.saveCookie(self.username)
随机数
**def ****random**(self,num=16):
**return **’’.join([str(random.randint(0,9)) [b]for x **in **xrange(num)])
获取你关注的更新
**def ****getFollow**(self,pageLength=10,pageNumber=1):
reptime=str(time.time()).replace(’.’,’’)
addr=‘http://api.bilibili.com/x/feed/pull?callback=jQuery1720’+self.random(16)+’’+reptime+‘0&jsonp=jsonp&ps=’+str(pageLength)+’&pn=’+str(pageNumber)+’&type=0&=’+reptime+‘9’
ret=self.brow.read(addr)
ret=ret[ret.index(’(’)+1:-1]
**return **ret
回复某帖子
**def ****reply**(self,aid,message):
postdata=urllib.urlencode({
‘oid’ :aid,
‘type’ :‘1’,
‘message’:message.encode(‘gbk’),
‘plat’ :‘1’,
‘jsonp’ :‘jsonp’
})
try:
**if **json.loads(self.brow.open(‘http://api.bilibili.com/x/v2/reply/add’,postdata).read())'message’] == “”:
**return **True
except:
**return **False
# 回复帖子里的回复
**def **replyReply(self, aid, rid_f, rid_s, message):
postdata=urllib.urlencode({
‘oid’ :aid,
‘type’ :‘1’,
‘root’:rid_f,
‘parent’:rid_s,
‘message’:message.encode(‘gbk’),
‘plat’ :‘1’,
‘jsonp’ :‘jsonp’
})
try:
**if **json.loads(self.brow.open(‘http://api.bilibili.com/x/v2/reply/add’,postdata).read())'message’] == “”:
**return **True
except:
**return **False
# 点赞
**def **fabulous(self, aid, rid):
postdata = urllib.urlencode({
‘oid’: aid,
‘type’: ‘1’,
‘rpid’:rid,
‘action’: ‘1’,
‘jsonp’: ‘jsonp’
})
try:
**if **json.loads(self.brow.open(‘http://api.bilibili.com/x/v2/reply/add’,postdata).read())'message’] == “”:
**return **True
except:
**return **False
# 投币
**def **coin(self,aid,num=1):
postdata=urllib.urlencode({
‘aid’:aid,
‘rating’:‘100’,
‘player’:‘1’,
‘multiply’:num
})
**return **self.brow.open(‘http://www.bilibili.com/plus/comment.php’,postdata).read()=='OK’
# 获取回复
**def **getReply(self,aid,pagenum=1):
reptime = str(time.time()).replace(’.’, ‘’)
url=‘http://api.bilibili.com/x/v2/reply?callback=jQuery1720’+self.random(16)+’’+reptime+‘0&jsonp=jsonp&pn=’+str(pagenum)+’&type=1&oid=’+str(aid)+’&sort=0&=’+reptime+‘9’
data=self.brow.read(url)
data=data[data.index(’(’)+1:-1]
**return **json.loads(data)
获取回复数
**def ****getReplyNum**(self,aid):
**return **self.getReply(aid)‘data’]‘page’]‘acount’]
写成了类,方便大家号多,实例化一个对象就是一个号,这个在登陆成功之后会保存cookie的,cookie的名字就是账户名,在创建对象的时候也会试图读取和用户名的文件做cookie
user=Bilibili(‘username’,‘password’)
**if not **user.testLogin():
user.getVcode()
vcode = raw_input(‘vcode:’)
**print **user.login(vcode)
**print **user.getFollow()
user.getReplyNum(‘8114028’)
上面就是一个简单的代码调用
getFollow函数的返回为json,格式如下
{
“code”:0,
“data”:{
“feeds”:
{
“id”:290758,
“src_id”:31682349,
“add_id”:8132488,
“type”:1,
“mcid”:0,
“ctime”:1484977999,
“source”:{
“mid”:“31682349”,
“uname”:“陛下开饭了”,
“sex”:“保密”,
“sign”:“你的吃相,是最美的模样,小笼包餐见陛下们,里面请!”,
“avatar”:“http://i1.hdslb.com/bfs/face/6e07c064c3c645f5436a51a012a71f5476de73b4.jpg”,
“rank”:“10000”,
“DisplayRank”:“0”,
“level_info”:{
“current_level”:3,
“current_min”:1500,
“current_exp”:3859,
“next_exp”:4500
},
“pendant”:{
“pid”:0,
“name”:"",
“image”:"",
“expire”:0
},
“nameplate”:{
“nid”:0,
“name”:"",
“image”:"",
“image_small”:"",
“level”:"",
“condition”:""
},
“official_verify”:{
“type”:-1,
“desc”:""
}
},
“addition”:{
“aid”:8132488,
“typeid”:76,
“typename”:“美食圈”,
“title”:"【 厨房三十六技】——万能系列",
“subtitle”:"",
“play”:0,
“review”:0,
“video_review”:0,
“favorites”:0,
“mid”:31682349,
“author”:“陛下开饭了”,
“link”:“http://www.bilibili.com/video/av8132488/”,
“keywords”:“生活,厨房,原创,技巧,万能系列,古风,美食”,
“description”:"【万能系列】万能系列,搞定一切菜和饭~非常适合平时忙却没有时间做饭的上班族!生活匆忙也要照顾好自己!关注陛下开饭了微信(ID:bixiakaifanle)每天都有好吃的!!",
“create”:“2017-01-21 13:44”,
“pic”:“http://i0.hdslb.com/bfs/archive/5af68955aa07ae85ead3f3618a3f92be02963588.jpg_320x200.jpg”,
“credit”:0,
“coins”:0,
“money”:0,
“duration”:“3:22”,
“status”:0,
“view”:0,
“view_at”:"",
“fav_create”:0,
“fav_create_at”:"",
“flag”:“p”
}
},{}//…同上格式,为关注的列表前几个,具体是几个获取的时候指定参数
],
“page”:{
“count”:180,
“num”:1,
“size”:10
}
}
}
获取评论的里面我就没仔细看,貌似在获取评论的同时还会获取每个用户信息
这个aid都是通用的,就是av什么东西的那一串数字,只有那个数字是aid,,不带av开头。。
这个rid是回复帖子的id,replyReply这个拗口的函数是用来回复别人回复里面的回复的,,,,
就是
帖子1
帖子1的回复
帖子1回复的回复(我们就是回复这个玩意)
然后两个回复都是有id的,这个id就是rid,rid_f是外层回复,rid_s是内层的回复
祝大家新年快乐啊