我正在尝试登录到一个非常简单的Web界面。这应该涉及输入和提交密码;我不希望需要跟踪cookie并且没有用户名。如何将密码输入到网页表单中并使用Python进行发布?
的网页是类似以下,以一个简单的形式张贴密码:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<!-- saved from url=(0039)http://start.ubuntu.com/wless/index.php -->
<html><head><meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Wireless Authorisation Page</title>
</head>
<body>
<h1>Title</h1>
<h2>Wireless Access Authorisation Page</h2>
Hello<br>
<form action="http://start.ubuntu.com/wless/index.php" method="POST"><input type="hidden" name="action" value="auth">PIN: <input type="password" name="pin" size="6"><br><input type="submit" value="Register"></form>
<h3>Terms of use</h3><p>some text</p>
</body>
</html>
我试图使用的urllib和urllib2的以下内容:
import urllib
import urllib2
URL = "http://start.ubuntu.com/wless/index.php"
data = urllib.urlencode({"password": "verysecretpasscode"})
response = urllib2.urlopen(URL, data)
response.read()
这没有奏效(返回相同的页面并且登录不成功)。我可能会在哪里出错?
@IsaacDj谢谢你的建议。硒对于这项任务来说似乎过度。当真正需要的时候,我并不是真的想要打开一个完整的浏览器,而是在后台进行这种操作。 – BlandCorporation
http://docs.python-requests.org/en/latest/index.html 请求应该是你在这种情况下去模块 – IsaacDj
“这没有工作”是一个完全无用的描述你的问题。 –