Python urllib和身份验证

时间:2011-02-28 20:28:27

标签: python authentication urllib

我需要帮助使用urllib验证登录此站点的帮助。我正在使用python 3,但我愿意恢复到2.x. 这就是我所熟悉的(基本上来自文档),它没有错误,但它没有登录。

file =open("loggedinsource.html",'wb')

# Create an OpenerDirector with support for Basic HTTP Authentication...
auth_handler = urllib.request.HTTPBasicAuthHandler()
auth_handler.add_password(realm='kalahari.net',uri='https://www.kalahari.net/profile/pipeline/signin.aspx?',user='myuser',passwd='mypass')

opener = urllib.request.build_opener(auth_handler)

# ...and install it globally so it can be used with urlopen.
urllib.request.install_opener(opener)
f=urllib.request.urlopen('https://www.kalahari.net/profile/pipeline/signin.aspx?')

page=f.read()
file.write(page);
file.close()

我一直在努力解决这个问题,请帮忙。

1 个答案:

答案 0 :(得分:1)

您尝试登录的网站未使用HTTP基本身份验证;它使用的是常规HTML表单。

如果你想登录这样的东西,你可能想看看mechanize

之类的东西