国产成人精品久久免费动漫-国产成人精品天堂-国产成人精品区在线观看-国产成人精品日本-a级毛片无码免费真人-a级毛片毛片免费观看久潮喷

您的位置:首頁技術文章
文章詳情頁

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

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

寫在前面

作為一名找不到工作的爬蟲菜雞人士來說,登陸這一塊肯定是個比較大的難題。 從今天開始準備一點點對大型網站進行逐個登陸破解。加深自己爬蟲水平。

環境搭建

Python 3.7.7環境,Mac電腦測試 Python內置庫 第三方庫: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() ’’’登錄函數’’’ def login(self, username, password, crack_captcha_func=None, **kwargs): # 若參數中給入代理,則設置 self.session.proxies.update(kwargs.get(’proxies’, {})) # 是否需要驗證碼 is_need_captcha = False while True: # 需要驗證碼 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() # 不需要驗證碼, 登錄成功 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 # 需要識別驗證碼 elif response_json[’code’] == -105:is_need_captcha = True # 賬號密碼錯誤 elif response_json[’code’] == -629:raise RuntimeError(’Account -> %s, fail to login, username or password error’ % username) # 其他錯誤 else:raise RuntimeError(response_json.get(’message’)) ’’’計算sign值’’’ def __calcSign(self, param, salt='560c52ccd288fed045859ed18bffd973'): 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’} # 獲取key密鑰URL self.getkey_url = ’https://passport.bilibili.com/api/oauth2/getKey’ # 獲取登陸URL self.login_url = ’https://passport.bilibili.com/api/v3/oauth2/login’ # 獲取驗證碼URL self.captcha_url = ’https://passport.bilibili.com/captcha’ # 破解網站來自: https://github.com/Hsury/Bilibili-Toolkit # 破解驗證碼URL self.crack_captcha_url = ’https://bili.dev:2233/captcha’ # 請求頭都得加這個 self.session.headers.update({’User-Agent’: 'Mozilla/5.0 BiliDroid/5.51.1 ([email protected])'})

移動端登陸

移動端與PC端類似,網址URL差異以及請求頭差異。在此不過多介紹。 全部代碼:

’’’移動端登錄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() ’’’登錄函數’’’ def login(self, username, password, crack_captcha_func=None, **kwargs): self.session.proxies.update(kwargs.get(’proxies’, {})) # 是否需要驗證碼 is_need_captcha = False while True: # 需要驗證碼 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() # 不需要驗證碼, 登錄成功 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 # 需要識別驗證碼 elif response_json[’code’] == -105:is_need_captcha = True # 賬號密碼錯誤 elif response_json[’code’] == -629:raise RuntimeError(’Account -> %s, fail to login, username or password error’ % username) # 其他錯誤 else:raise RuntimeError(response_json.get(’message’)) ’’’計算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’ # 破解網站來自: 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 ([email protected])'})

到此這篇關于Python爬蟲破解登陸嗶哩嗶哩的方法的文章就介紹到這了,更多相關Python爬蟲破解登陸內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!

標簽: python
相關文章:
主站蜘蛛池模板: www.黄免费| 国产91丝袜在线播放九色 | 国产成人精品日本亚洲语音1 | 久久99精品久久只有精品 | 新版天堂中文资源官网 | 国产成人精品视频播放 | 国产一级网站 | 亚洲欧洲eeea在线观看 | 美国免费一级片 | 九九在线免费观看视频 | 欧美在线播放成人免费 | 国产99久久精品 | 亚洲欧美日韩久久精品第一区 | 中文字幕精品在线观看 | 热99re久久精品香蕉 | 亚洲一区二区久久 | 久久精品国产亚洲a | 精品国产日韩亚洲一区在线 | 欧美三级aaa | 日本毛片在线看 | 手机av在线播放 | 久久999精品| 欧美大胆一级视频 | 亚洲综合视频网 | 亚洲色色色图 | 91热国内精品永久免费观看 | 国产精品爱久久久久久久 | 欧美在线国产 | 男女免费观看视频 | 国产福利在线91 | 日韩福利视频精品专区 | 日本加勒比在线观看 | 国产欧美日韩另类 | 免费成年人在线视频 | 国产欧美日韩亚洲精品区2345 | 性做久久久久免费看 | 成人亚洲国产 | 国产精品视频一区二区猎奇 | 香蕉99国内自产自拍视频 | 美女黄频免费看 | 在线播放日韩 |