如何将密码输入网页表单并发布到 Python?

How can a passcode be entered into a web page form and POSTed in Python?

我正在尝试登录一个非常简单的网络界面。这应该包括输入和提交密码;我不希望需要跟踪 cookie,也没有用户名。

网页如下,有一个简单的 POST 密码表格:

<!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()

这没有用(返回相同的页面并且登录不成功)。我可能哪里出错了?

您可能想尝试使用 requests

这允许您

import requests
print(requests.post(url, data={"password": "verysecretpasscode"}))

表单有两个命名输入字段,您只发送一个:

<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>

第二个称为 pin,而不是 password,因此您的数据字典应如下所示:

{"pin": "verysecretpasscode", "action": "auth"}