python机械化处理具有相同名称的两个参数 [英] python mechanize handle two parameters with same name

查看:98
本文介绍了python机械化处理具有相同名称的两个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在登录一个页面,其中他们奇怪地具有一个名为login_email的表单输入和两个名为login_password的表单输入.我需要设置两者的值,但是直接调用form['login_password']会引发错误:

I'm logging into a page where they oddly have a form input called login_email and two form inputs called login_password. I need to set the value of both but the straightforward call form['login_password'] throws an error:

  File "/Library/Python/2.7/site-packages/mechanize/_form.py", line 3101, in find_control
    return self._find_control(name, type, kind, id, label, predicate, nr)
  File "/Library/Python/2.7/site-packages/mechanize/_form.py", line 3183, in _find_control
    raise AmbiguityError("more than one control matching "+description)
mechanize._form.AmbiguityError: more than one control matching name 'login_password'

我只需要找到一种同时提交form['login_password'] = "Password"form['login_password'] = "monkeybutler"的方法.我没有在Browser对象中看到变量来更改POST数据参数.

I just need to find a way to submit form['login_password'] = "Password" and form['login_password'] = "monkeybutler" at the same time. I'm not seeing a variable in the Browser object to change the POST data params.

有什么建议吗? 这是我尝试失败的尝试:

Any suggestions? Here's what I tried without success:

# Select the first (index zero) form
br.select_form(nr=0)

# Let's search
br.form['login_email'] = 'mommajane@gmail.com'

#my_fields = br.form.fields.select
#my_fields[0].login_password = "Password"
#my_fields[1].login_password = "123qwerty"
br.form['login_password']= ['Password','123qwerty']

br.submit()

推荐答案

如果您面对两个具有相同名称,id等的字段,则必须使用一些变通办法,尽管它不是很干净

If you are facing two fields with the same name, id and so on, you must use a little workaround, altough its not very clean

首先,我为该示例定义了一个简单的html文件,因为我不知道您使用的URL:

First I have defined a simple html file for that example since I did not know the URL you used:

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
       "http://www.w3.org/TR/html4/strict.dtd">
<html>
<head>
<title>foo</title>
</head>
<body>

<h1>bar</h1>

<form action="input_text.htm">
  <p>name:<br><input name="name" type="text" size="30" maxlength="30"></p>
  <p>sec_name:<br><input name="sec_name" type="text" size="30" maxlength="40"></p>
  <p>sec_name:<br><input name="sec_name" type="text" size="30" maxlength="40"></p>
</form>

</body>
</html>

然后,我可以使用以下python代码将值快速又脏地插入到这些字段中:

Afterwards I was able to insert values into those fields quick and dirty by using this python code:

>>> import mechanize
>>> browser = mechanize.Browser()
>>> browser.open("file:///home/foo/index.html")
<response_seek_wrapper at 0x229a7e8 whose wrapped ...
>>> browser.select_form(nr=0)
>>> name = 'foo'
>>> for control in browser.form.controls:
...     if control.name == 'sec_name':
...             control.value = name
...             name = 'bar'
... 
>>> for control in browser.form.controls:
...     print control
... 
<TextControl(name=)>
<TextControl(sec_name=foo)>
<TextControl(sec_name=bar)>
>>> 

不好,但是可以用.希望能有所帮助.

It`s not nice but it works. Hope that helped.

这篇关于python机械化处理具有相同名称的两个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆