我正在嘗試登錄到一個非常簡單的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
「這沒有工作」是一個完全無用的描述你的問題。 –