python爬蟲時web客戶端授權驗證處理器的使用

from urllib.request import HTTPPasswordMgrWithDefaultRealm,HTTPBasicAuthHandler,build_opener

username=“username”
password=“password”
url=" "
#生成密碼管理器對象
passwordMgr=HTTPPasswordMgrWithDefaultRealm()
#往對裏面添加賬號,密碼已經url
passwordMgr.add_password(None,url,username,password)
#創建處理驗證的handler 參數爲 HTTPPasswordMgrWithDefaultRealm對象
auth_Handler=HTTPBasicAuthHandler(passwordMgr)
#創建opener,並且發送請求
opener=build_opener(auth_Handler)
opener.open(url)

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章