2010-01-28 29 views
3

我正嘗試使用Python登錄到網站。我編寫了代碼連接到目標,但我需要登錄並選擇網站上的按鈕並等待響應。我已經看過Python中的HTTP協議,並且正在考慮使用'HTTPConnection.putrequest'。我不知道如何做到這一點,我有我到目前爲止下面的代碼:登錄網站並使用Python選擇選項

def testHTTPS(self): 

    c = httplib.HTTPSConnection(ip) 
    c.request("GET", "/") 
    response = c.getresponse() 
    self.assertEqual(response.status, 200) # '200' is success code 
    conn.close() 

而對於網站上的登錄功能的代碼是:

<td align="right" id="lgn_userName"></td> 
    <td><input type="text" class="button" name="username" id="username" size="24" maxlength="16" accesskey="u" tabindex="1" value=""/></td> 
    </tr> 

    <tr> 
     <td align="right" id="lgn_userPwd"></td> 
     <td><input type="password" class="button" name="password" id="password" size="24" maxlength="20" accesskey="p" tabindex="2" value=""/></td> 
    </tr> 

    <tr> 
     <td align="right">&nbsp;</td> 
     <td> 
     <input type="submit" id="lgn_button" class="button" tabindex="3" accesskey="s" /> 
     </td> 

有誰知道如何去做這個?

感謝

回答

3

是的,你用mechanize,其中一個不大不小的 「網頁瀏覽器」 爲Python的。有了它,您可以輕鬆打開網頁,查找表單,填寫表單值並從Python提交表單。我使用它(通過Zopes testbrowser模塊)來測試Web應用程序。