2017-05-30 14 views
-2

我正在尝试登录到一个非常简单的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() 

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

+0

@IsaacDj谢谢你的建议。硒对于这项任务来说似乎过度。当真正需要的时候,我并不是真的想要打开一个完整的浏览器,而是在后台进行这种操作。 – BlandCorporation

+0

http://docs.python-requests.org/en/latest/index.html 请求应该是你在这种情况下去模块 – IsaacDj

+0

“这没有工作”是一个完全无用的描述你的问题。 –

回答

3

表单具有两个名为输入字段,你只发送一个:

<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"} 
+0

繁荣,就是这样。非常感谢您解释发生了什么问题。 – BlandCorporation

1

您可能需要使用一些尝试像requests

这可以让你

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

感谢您的建议。我已经给出了一个尝试,但它似乎没有工作。我得到一个响应200,然后返回相同的网页,登录不成功。 – BlandCorporation

相关问题