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)