RoboBrowser BadRequestKeyError(key)

时间:2016-06-01 18:26:45

标签: python-3.x werkzeug robobrowser

我正在尝试使用RoboBrowser登录网站,但我遇到了错误消息。

我的代码:

from robobrowser import RoboBrowser

browser = RoboBrowser()

def login():
    browser.open('https://www.kijiji.ca/t-login.html')
    form = browser.get_form(id="login-form")
    form.fields["LoginEmailOrNickname"].value = "an_email_address"
    form.fields["login-password"].value = "a_password"
    form.fields["login-rememberMe"].value = "true"
    browser.submit_form(form)

login()  

错误消息:

  Traceback (most recent call last):
  File "/home/rojaslee/Desktop/kijiji_poster/kijiji_poster.py", line 16, in <module>
    login()
  File "/home/rojaslee/Desktop/kijiji_poster/kijiji_poster.py", line 11, in login
    form.fields["LoginEmailOrNickname"].value = ["an_email_address"]
  File "/usr/local/lib/python3.4/dist-packages/werkzeug/datastructures.py", line 744, in __getitem__
    raise exceptions.BadRequestKeyError(key)
    werkzeug.exceptions.BadRequestKeyError: 400: Bad Request

3 个答案:

答案 0 :(得分:1)

旧线程,但我一直在努力解决同样的问题。这对我有用:

from robobrowser import Robobrowser

browser = RoboBrowser

def login():
    browser.open('https://www.kijiji.ca/t-login.html')
    form = browser.get_form(id="login-form")
    form["EmailOrNickname"].value = "an_email_address"
    form["password"].value = "a_password"
    form["rememberMe"].value = "checked"
    browser.submit_form(form)

答案 1 :(得分:0)

您要登录的网站的HTML代码如下:

<section>
  <label for="LoginEmailOrNickname">Email Address or Nickname:</label>
  <input id="LoginEmailOrNickname" name="emailOrNickname" req="req" type="text" value="" maxlength="128"><span class="field-message" data-for="LoginEmailOrNickname"></span>
</section>
<section>
  <label for="login-password">Password:</label>
  <input id="login-password" name="password" req="req" type="password" value="" maxlength="64"><span class="field-message" data-for="login-password"></span>
  <a id="LoginForgottenPassword" href="/t-forgot-password.html">Forgot your password?</a>
</section>

要在表单字段中添加值,您必须获取名称属性,而不是ID。

此代码应该有效:

form.fields["emailOrNickname"].value = "an_email_address"
form.fields["password"].value = "a_password"
form.fields["rememberMe"].value = "true"

如果您需要获取表单的字段,可以打印它们:

print form.fields

答案 2 :(得分:0)

但是你尝试使用&#39;试试除了&#39;?

我能够使用以下方法解决此错误:

try:
    form['userid'] = 'my_username'
    form['password'] = 'my_password'
except Exception:
    pass

我使用的网站与您不同,因此请确保输入正确的表单字段。

让我知道这是否有帮助!