如何使用 Python 登录到简单的 Web 访问登录名?

How can I log into a simple web access login using Python?

我正在尝试创建一个小 Python 脚本,为了方便起见,它会自动为我登录到 Web 访问身份验证页面(每次计算机与网络断开连接时都会出现登录信息) ).

到目前为止,我一直尝试使用 mechanize 模块,但是 运行 这不会导致登录从我的标准浏览器中消失:

import mechanize
browser = mechanize.Browser()
browser.addheaders = [("User-agent","Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.2.13) Gecko/20101206 Ubuntu/10.10 (maverick) Firefox/3.6.13")]
browser.open("https://controller.mobile.lan/101/portal/")
browser.select_form(name="logonForm")
browser["login"] = "myUsername"
browser["password"] = "myPasscode"
browser.submit()
print browser.title()

如何让此登录在 Python 中工作?

以下是我认为是登录页面 HTML 的相关部分:

<form name="logonForm" style="display:none">
    <!-- Logon Form -->
    <div id="logonForm_subscriptionChoice_top_title_block" class="subtitle">
        <span id="logonForm_subscriptionChoice_top_title_text">YOU ALREADY HAVE YOUR LOGIN</span>
    </div>
    <div id="logonForm_auth_modes_block" style="display:none">
    <table class="hoverLink"><tr>
<td>
                <div id="logonForm_shibboleth_authentication_button">
                    <img src="./resources/_images/shibboleth.png" height="30px"><br><span id="logonForm_shibboleth_text">Utilisez vos identifiants institutionnels</span>
                </div>
            </td>
            <td>
                <div id="logonForm_standard_authentication_button">
                    <img src="./resources/_images/ticket.png" height="30px"><br><span id="logonForm_ticket_text">Utilisez un ticket de connexion</span>
                </div>
            </td>
        </tr></table>
</div>
    <div id="logonForm_logon_block">
        <table>
<tr id="logonForm_logon_block_credentials">
<td class="label">
                    <span id="logonForm_login_text">LOGIN</span><br><input type="text" name="login" autocomplete="on">
</td>
                <td class="label">
                    <span id="logonForm_password_text">PASSWORD</span><br><input type="password" name="password" autocomplete="on">
</td>
                <td>
                    <button type="submit" id="logonForm_connect_button"><span><img src="./resources/_images/auth_button.png" height="35px"></span></button>
                </td>
            </tr>
<tr id="logonForm_policy_block">
<!-- Check Box Confirm (Visible status depends on configuration option) --><td colspan="3">
                    <br><input type="checkbox" name="policy_accept">&nbsp;
                    <span id="logonForm_policy_text"></span>
                </td>
            </tr>
</table>
</div>
    <br><button type="button" id="logonForm_authentication_form_back_button" style="display:none">Retour</button>
    <div id="logonForm_subscriptionChoice_block">
        <br><div class="subtitle">
            <span id="logonForm_subcribe_bottom_title_text">NOT A LOGIN YET ?</span>
        </div>
        <br><div id="logonForm_subscriptionChoice_first_double_insert_block">
            <table class="hoverLink"><tr>
<td></td>
<td></td>
</tr></table>
</div>
        <div id="logonForm_subscriptionChoice_second_double_insert_block">
            <table class="hoverLink"><tr>
<td></td>
<td></td>
</tr></table>
</div>
        <div id="logonForm_subscriptionChoice_single_insert_block">
            <table class="hoverLink"><tr><td></td></tr></table>
</div>
    </div>
</form>

该表单在某处提交数据。您需要找出它使用的位置和方法。找到后,你可以使用requests库来做一行,如:

response = requests.post("https://controller.mobile.lan/101/portal/", data={'login': "username", 'password': "password")
print response.read() # Dumps the whole webpage after.

请注意,如果该表单使用 javascript 进行提交,那么 mechanize 将不会执行此操作,您必须获得真正使 javascript 生效的内容。 Mechanize 的常见问题解答 (here) 指定它不会执行 javascript,您必须在自己的代码中模拟它。

编辑:如果你身边有 PyQt4,或者可以安装它,你可以使用 'headless' 浏览器,像这样:

import sys  
from PyQt4.QtGui import QApplication  
from PyQt4.QtCore import QUrl
from PyQt4.QtWebKit import QWebPage

# Set vars here for convenience
username = "myUsername"
password = "myPassword"

class HeadlessBrowser(QWebPage):
    def __init__(self, url):
        self.app = QApplication(sys.argv)
        super(HeadlessBrowser, self).__init__()
        self.loadFinished.connect(self.login)
        self.mainFrame().load(QUrl(url))
        self.app.exec_();

    def login(self):
        doc = self.mainFrame().documentElement()
        user = doc.findFirst("input[name=login]")
        pwd = doc.findFirst("input[name=password]")
        button = doc.findFirst("button[id=logonForm_connect_button]")

        user.setAttribute("value", username)
        pwd.setAttribute("value", password)
        button.evaluateJavaScript("this.click()")
        # Uncomment if the button click above is not enough
        #form = doc.findFirst("form[name=logonForm]")
        #form.evaluateJavaScript("this.submit()")
        self.app.quit()

page = HeadlessBrowser("http://localhost/~iskren/headlesstest.html")
html = page.mainFrame().toHtml()

以及我用来测试的http://localhost/~iskren/headlesstest.html的内容:

<html>
<body>
<form name="logonForm"> 
        <input type="text" name="login"/>
        <input type="password" name="password"/>
        <button type="submit" id="logonForm_connect_button">Click me!</button>
</form>
</body>
</html>