我试图加载一个网页,但我遇到了这个问题。我确实有用户名和密码,但我不知道如何在 python 代码中使用它们。我查了python教程,这就是我写的:
import urllib2
password_mgr = urllib2.HTTPPasswordMgrWithDefaultRealm()
username = 'user'
password = 'pass'
top_level_url = "www.something.com:80"
password_mgr.add_password(None, top_level_url, username, password)
handler = urllib2.HTTPBasicAuthHandler(password_mgr)
opener = urllib2.build_opener(handler)
opener.open('http://www.something.com/h/h.html')
urllib2.install_opener(opener)
response = urllib2.urlopen()
page = response.read()
print page
哪里不对了?