【问题标题】:python's mechanize and forms: javascript string returnedpython的机械化和形式:返回的javascript字符串
【发布时间】:2014-09-25 15:00:05
【问题描述】:

我正在尝试通过 Mechanize 访问 http://forum.kriminala.net 并解析我的收件箱消息。

从html代码可以看出登录表单在主页面的嵌套iframe中:

<iframe src="login/" style="width: 100%; height: 124px; border-bottom: 2px solid #DDE5EA; box-shadow: 0px 0px 10px #ccc;" frameborder="0" vspace="0" scrolling="no" hspace="0">
...
<form action="" class="auth_form" method="post">
<input type="hidden" name="referer" value="http%3A%2F%2Fforum.kriminala.net%2F">
<input type="text" class="text_input" name="username" placeholder="Имя пользователя" value="" tabindex="1">
<input type="password" class="text_input" name="password" placeholder="Пароль" tabindex="2">
<input type="checkbox" id="autologin" checked="checked" name="autologin" tabindex="3">
<label for="autologin">Запомнить меня</label>
<input type="submit" class="submit_button" id="submit_button" name="login" value="" tabindex="3">
</form>
...
</iframe>

所以我导航到http://forum.kriminala.net/login,在那里找到表单并使用我的用户名和密码提交,将结果输出到一个文件中(查看我是否成功登录)。

br=mechanize.Browser()
br.open("http://forum.kriminala.net/login/")
br.select_form(nr=0)
br["username"]="12n"
br["password"]="123456"
response=br.submit()
htmlpage=open("response.html","w")
htmlpage.writelines(response.get_data())
htmlpage.close()

但是,我在文件中看到的只是:

<script type="text/javascript">
window.top.location = 'http://forum.kriminala.net/';
</script>

我接下来的想法可能是我应该手动去主法师,所以我在 Mechanize 中打开主页面,将其放入一个 html 文件中以在浏览器中打开,但是该文件看起来仍然像我没有登录一样。

我该如何处理?

附:我是一个完整的 Python 菜鸟,所以也许我只是不知道用谷歌搜索什么来得到我的答案。如果是这种情况,请指出正确的方向。

谢谢!

【问题讨论】:

    标签: javascript python html authentication mechanize


    【解决方案1】:

    一切似乎都很好。生成的页面使用 JavaScript 将您重定向到主页(或可能重定向到您登录之前所在的任何位置),这是一个合理的做法。由于您的“浏览器”中没有 JavaScript,因此您需要手动导航到任何需要的地方。

    登录的实际结果应该是其中一个响应中的Set-Cookie: 标头。您需要在后续请求标头中使用该 cookie 以使服务器认为您已登录。有关更多理论,请参阅HTTP cookie@wikipedia。

    Emulating a Browser in Python with mechanize 似乎有相关代码可以在机械化中执行此操作,特别是 br.set_cookiejar() 命令。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-08-13
      • 1970-01-01
      • 1970-01-01
      • 2013-06-09
      • 2015-10-26
      • 2014-04-26
      • 1970-01-01
      相关资源
      最近更新 更多