2017-05-30 39 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

相關問題