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

網頁爬蟲 - 用Python3的requests庫模擬登陸Bilibili總是提示驗證碼錯誤怎么辦?

瀏覽:99日期:2022-06-30 16:58:08

問題描述

我懷疑是驗證碼和對應的cookies不同步,但我也檢查不出來哪里錯了代碼如下,望哪位大神幫我看看

#!/usr/bin/python# -*- coding: utf-8 -*-import requestsimport chardetimport osfrom PIL import Imagefrom io import BytesIO def login(): #發送登錄請求的目標地址 url = ’https://passport.bilibili.com/login/dologin’ #發送登錄請求所需參數 act = ’login’ gourl = ’https://passport.bilibili.com/login/dologin’ keeptime = ’2592000’ userid = ’*****’ pwd = ’*****’ #獲取驗證碼 r = requests.get(’https://passport.bilibili.com/captcha’) i = Image.open(BytesIO(r.content)) i.show() vdcode = input(’請手動輸入驗證碼:n’) #獲取與驗證碼同步的cookies cookies = dict(r.cookies) #封裝所需參數 data = {’act’:act,’gourl’:gourl,’keeptime’:keeptime,’userid’:userid,’pwd’:pwd,’vdcode’:vdcode} #偽裝成瀏覽器 headers = {’User-Agent’:’Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36’,’Referer’:’https://passport.bilibili.com/login’} #發送請求 r = requests.post(url,data=data,headers=headers,cookies=cookies) #通過返回的html代碼判斷是否登陸成功 content = r.content.decode() print(content)if __name__ == ’__main__’: login()

問題解答

回答1:

參考

標簽: Python 編程
相關文章:
国产综合久久一区二区三区