亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術(shù)文章
文章詳情頁

Python爬蟲破解登陸嗶哩嗶哩的方法

瀏覽:2日期:2022-07-05 10:47:14

寫在前面

作為一名找不到工作的爬蟲菜雞人士來說,登陸這一塊肯定是個(gè)比較大的難題。 從今天開始準(zhǔn)備一點(diǎn)點(diǎn)對(duì)大型網(wǎng)站進(jìn)行逐個(gè)登陸破解。加深自己爬蟲水平。

環(huán)境搭建

Python 3.7.7環(huán)境,Mac電腦測(cè)試 Python內(nèi)置庫 第三方庫:rsa、urllib、requests

PC端登陸

全部代碼:

’’’PC登錄嗶哩嗶哩’’’class Bilibili_For_PC(): def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) self.session = requests.Session() self.__initialize() ’’’登錄函數(shù)’’’ def login(self, username, password, crack_captcha_func=None, **kwargs): # 若參數(shù)中給入代理,則設(shè)置 self.session.proxies.update(kwargs.get(’proxies’, {})) # 是否需要驗(yàn)證碼 is_need_captcha = False while True: # 需要驗(yàn)證碼 if is_need_captcha:captcha_img = self.session.get(self.captcha_url, headers=self.captcha_headers).contentdata = {’image’: base64.b64encode(captcha_img).decode(’utf-8’)}captcha = self.session.post(self.crack_captcha_url, json=data).json()[’message’] # 獲得key值 appkey = ’1d8b6e7d45233436’ data = { ’appkey’: appkey, ’sign’: self.__calcSign(’appkey={}’.format(appkey)) } response = self.session.post(self.getkey_url, data=data) response_json = response.json() key_hash = response_json[’data’][’hash’] pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(response_json[’data’][’key’].encode(’utf-8’)) # 模擬登錄 if is_need_captcha:data = 'access_key=&actionKey=appkey&appkey={}&build=6040500&captcha={}&challenge=&channel=bili&cookies=&device=pc&password={}&permission=ALL&seccode=&subid=1&ts={}&username={}&validate=' .format(appkey, captcha, urllib.parse.quote_plus(base64.b64encode(rsa.encrypt(’{}{}’.format(key_hash, password).encode(), pub_key))), int(time.time()), urllib.parse.quote_plus(username)) else:data = 'access_key=&actionKey=appkey&appkey={}&build=6040500&captcha=&challenge=&channel=bili&cookies=&device=pc&password={}&permission=ALL&seccode=&subid=1&ts={}&username={}&validate=' .format(appkey, urllib.parse.quote_plus(base64.b64encode(rsa.encrypt(’{}{}’.format(key_hash, password).encode(), pub_key))), int(time.time()), urllib.parse.quote_plus(username)) data = '{}&sign={}'.format(data, self.__calcSign(data)) response = self.session.post(self.login_url, data=data, headers=self.login_headers) response_json = response.json() # 不需要驗(yàn)證碼, 登錄成功 if response_json[’code’] == 0 and response_json[’data’][’status’] == 0:for cookie in response_json[’data’][’cookie_info’][’cookies’]: self.session.cookies.set(cookie[’name’], cookie[’value’], domain=’.bilibili’)print(’[INFO]: Account -> %s, login successfully’ % username)infos_return = {’username’: username}infos_return.update(response_json)return infos_return, self.session # 需要識(shí)別驗(yàn)證碼 elif response_json[’code’] == -105:is_need_captcha = True # 賬號(hào)密碼錯(cuò)誤 elif response_json[’code’] == -629:raise RuntimeError(’Account -> %s, fail to login, username or password error’ % username) # 其他錯(cuò)誤 else:raise RuntimeError(response_json.get(’message’)) ’’’計(jì)算sign值’’’ def __calcSign(self, param, salt='560c52ccd288fed045859ed18bffd973'): sign = hashlib.md5(’{}{}’.format(param, salt).encode(’utf-8’)) return sign.hexdigest() ’’’初始化’’’ def __initialize(self): # 登陸請(qǐng)求頭 self.login_headers = {’Content-type’: ’application/x-www-form-urlencoded’} # 破解驗(yàn)證碼請(qǐng)求頭 self.captcha_headers = {’Host’: ’passport.bilibili.com’} # 獲取key密鑰URL self.getkey_url = ’https://passport.bilibili.com/api/oauth2/getKey’ # 獲取登陸URL self.login_url = ’https://passport.bilibili.com/api/v3/oauth2/login’ # 獲取驗(yàn)證碼URL self.captcha_url = ’https://passport.bilibili.com/captcha’ # 破解網(wǎng)站來自: https://github.com/Hsury/Bilibili-Toolkit # 破解驗(yàn)證碼URL self.crack_captcha_url = ’https://bili.dev:2233/captcha’ # 請(qǐng)求頭都得加這個(gè) self.session.headers.update({’User-Agent’: 'Mozilla/5.0 BiliDroid/5.51.1 (bbcallen@gmail.com)'})

移動(dòng)端登陸

移動(dòng)端與PC端類似,網(wǎng)址URL差異以及請(qǐng)求頭差異。在此不過多介紹。 全部代碼:

’’’移動(dòng)端登錄B站’’’class Bilibili_For_Mobile(): def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) self.session = requests.Session() self.__initialize() ’’’登錄函數(shù)’’’ def login(self, username, password, crack_captcha_func=None, **kwargs): self.session.proxies.update(kwargs.get(’proxies’, {})) # 是否需要驗(yàn)證碼 is_need_captcha = False while True: # 需要驗(yàn)證碼 if is_need_captcha:captcha_img = self.session.get(self.captcha_url, headers=self.captcha_headers).contentdata = {’image’: base64.b64encode(captcha_img).decode(’utf-8’)}captcha = self.session.post(self.crack_captcha_url, json=data).json()[’message’] # 獲得key值 appkey = ’bca7e84c2d947ac6’ data = { ’appkey’: appkey, ’sign’: self.__calcSign(’appkey={}’.format(appkey)) } response = self.session.post(self.getkey_url, data=data) response_json = response.json() key_hash = response_json[’data’][’hash’] pub_key = rsa.PublicKey.load_pkcs1_openssl_pem(response_json[’data’][’key’].encode(’utf-8’)) # 模擬登錄 if is_need_captcha:data = 'access_key=&actionKey=appkey&appkey={}&build=6040500&captcha={}&challenge=&channel=bili&cookies=&device=phone&mobi_app=android&password={}&permission=ALL&platform=android&seccode=&subid=1&ts={}&username={}&validate=' .format(appkey, captcha, urllib.parse.quote_plus(base64.b64encode(rsa.encrypt(’{}{}’.format(key_hash, password).encode(), pub_key))), int(time.time()), urllib.parse.quote_plus(username)) else:data = 'access_key=&actionKey=appkey&appkey={}&build=6040500&captcha=&challenge=&channel=bili&cookies=&device=phone&mobi_app=android&password={}&permission=ALL&platform=android&seccode=&subid=1&ts={}&username={}&validate=' .format(appkey, urllib.parse.quote_plus(base64.b64encode(rsa.encrypt(’{}{}’.format(key_hash, password).encode(), pub_key))), int(time.time()), urllib.parse.quote_plus(username)) data = '{}&sign={}'.format(data, self.__calcSign(data)) response = self.session.post(self.login_url, data=data, headers=self.login_headers) response_json = response.json() # 不需要驗(yàn)證碼, 登錄成功 if response_json[’code’] == 0 and response_json[’data’][’status’] == 0:for cookie in response_json[’data’][’cookie_info’][’cookies’]: self.session.cookies.set(cookie[’name’], cookie[’value’], domain=’.bilibili’)print(’[INFO]: Account -> %s, login successfully’ % username)infos_return = {’username’: username}infos_return.update(response_json)return infos_return, self.session # 需要識(shí)別驗(yàn)證碼 elif response_json[’code’] == -105:is_need_captcha = True # 賬號(hào)密碼錯(cuò)誤 elif response_json[’code’] == -629:raise RuntimeError(’Account -> %s, fail to login, username or password error’ % username) # 其他錯(cuò)誤 else:raise RuntimeError(response_json.get(’message’)) ’’’計(jì)算sign值’’’ def __calcSign(self, param, salt='60698ba2f68e01ce44738920a0ffe768'): sign = hashlib.md5(’{}{}’.format(param, salt).encode(’utf-8’)) return sign.hexdigest() ’’’初始化’’’ def __initialize(self): self.login_headers = {’Content-type’: ’application/x-www-form-urlencoded’ } self.captcha_headers = {’Host’: ’passport.bilibili.com’ } self.getkey_url = ’https://passport.bilibili.com/api/oauth2/getKey’ self.login_url = ’https://passport.bilibili.com/api/v3/oauth2/login’ self.captcha_url = ’https://passport.bilibili.com/captcha’ # 破解網(wǎng)站來自: https://github.com/Hsury/Bilibili-Toolkit self.crack_captcha_url = ’https://bili.dev:2233/captcha’ self.session.headers.update({’User-Agent’: 'Mozilla/5.0 BiliDroid/5.51.1 (bbcallen@gmail.com)'})

到此這篇關(guān)于Python爬蟲破解登陸嗶哩嗶哩的方法的文章就介紹到這了,更多相關(guān)Python爬蟲破解登陸內(nèi)容請(qǐng)搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!

標(biāo)簽: python
相關(guān)文章:
主站蜘蛛池模板: 亚洲自拍偷拍视频 | 黄色小视频在线免费观看 | 成人99国产精品 | 日本人69视频在线观看 | 1300部小u女视频免费 | 日本香蕉一区二区在线观看 | 日韩免费a级在线观看 | 午夜啪啪免费视频 | 美日韩中文字幕 | 久久久久国产精品免费免费不卡 | 老司机一级毛片 | 手机看片欧美日韩 | 在线免费观看黄色片 | 特黄特黄特色大片免费观看 | 三级黄色片在线免费观看 | 免费国产成人手机在线观看 | 99ri精品国产亚洲 | 在线精品自拍亚洲第一区 | 国产系列在线播放 | xxx性欧美人 | 丝袜 亚洲 另类 欧美 变态 | 欧美成人h精品网站 | 亚洲国产一区二区三区 | 国产视频你懂的 | 香蕉福利久久福利久久香蕉 | 日韩免费观看 | 亚洲欧美高清 | 妞干网在线播放 | 久久精品第一页 | 国产日本高清 | 久久久91精品国产一区二区三区 | caoporen个人免费公开视频 | 久久精品一区二区三区不卡牛牛 | 免费欧美日韩 | 精品精品国产高清a毛片 | 美女zw喷水视频在线观看 | 国产精品剧情原创麻豆国产 | 日本aaaa特级毛片 | 国产精品品福利视频 | 欧美色成人tv在线播放 | 国产乱码精品一区二区三 |