无法使用来自 python 的 mechanize 登录。我究竟做错了什么?

Can't login using mechanize from python. What am I doing wrong?

我正尝试在 python 中使用 mechanize 登录此站点:https://login.haaretz.co.il/ 从表面上看,它看起来像是一个两阶段的登录过程,与 google 相同,但是通过 mechanize 收到 google 登录后的收据让我无处可去。在 submit()-ing 之后,浏览器似乎保留在同一页面上,只有一个表单包含单个 userName 控件。 我做错了什么?

>>> import mechanize
>>> br = mechanize.Browser()
>>> br.open('https://login.haaretz.co.il/')
<response_seek_wrapper at 0x7f53bfbc4a00 whose wrapped object = <closeable_response at 0x7f53bfbc4580 whose fp = <_io.BufferedReader name=3>>>
>>> 
>>> br.select_form(nr=0)
>>> 
>>> print(br.form)
<GET https://login.haaretz.co.il/ application/x-www-form-urlencoded
  <TextControl(userName=)>
  <IgnoreControl(<None>=<None>)>>
>>> br['userName']='my_email@gmail.com'
>>> resp = br.submit()
>>> # and after submitting I'm back a square one
>>> print(br.forms()[0])
<GET https://login.haaretz.co.il/?userName=my_email%40gmail.com application/x-www-form-urlencoded
  <TextControl(userName=)>
  <IgnoreControl(<None>=<None>)>>
>>> 

这是没有希望了吗?我做错了吗?

我的猜测是登录过程取决于JavaScript。如果登录依赖于 JavaScript,您将无法使用 Mechanize 获得想要的结果。参见 Mechanize and Javascript

xpath 'body/script[2]' 处的脚本标记有一个 JavaScript 对象和 'loginSuccess': False key:value 对。因此我的猜测是登录需要 JavaScript.