4

我正在尝试使用 Python 登录网站。我已经编写了连接到目标的代码,但我需要登录并选择网站上的一个按钮并等待响应。我查看了 Python 中的 HTTP 协议,并正在考虑使用“HTTPConnection.putrequest”。我不知道该怎么做,我的代码如下:

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>

有谁知道该怎么做?

谢谢

4

2 回答 2

4

是的,您使用mechanize,它是 Python 的一种“网络浏览器”。有了它,您可以轻松地打开网页、查找表单、填写表单值并从 Python 提交表单。我使用它(通过 Zopes testbrowser 模块)来测试 Web 应用程序。

于 2010-01-28T12:05:09.187 回答
1

使用urllib2并创建一个 POST 请求。

有关更多信息,请阅读:

于 2010-01-28T13:12:17.780 回答