使用机械化提交不带控件名称的表格 [英] Use mechanize to submit form without control name

查看:45
本文介绍了使用机械化提交不带控件名称的表格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用机械化的python提交表单,但是我需要填写的表单控件没有分配名称.

I'm trying to use mechanize for python to submit a form but the form control I need to fill in doesnt have a name assigned to it.

<POST https://sample.com/anExample multipart/form-data
  <HiddenControl(post_authenticity_token=) (readonly)>
  <HiddenControl(iframe_callback=) (readonly)>
  <TextareaControl(<None>=)>>

我要编辑的控件是上述对象<TextareaControl(<None>=)>中的最后一个控件.

The control I'm trying to edit is the last control in the above object, <TextareaControl(<None>=)>.

我看了看文档,似乎找不到一种为该控件分配值的方法,因为它没有与之关联的名称.

I've looked at the documentation and cant seem to find a way to assign a value to that control since it doesnt have a name associated with it.

  forms = [f for f in br.forms()]
  print forms[2].controls[5].name

输出无"

推荐答案

因此,我将授予您,这一点很难弄清楚.必须从字面上看一下 mechanize代码本身 .不幸的是,我无法在没有name属性的实际表单项上进行可靠的测试,尽管如果您提供要拉取的站点,也可以自己进行测试.

So, I'll grant you, this one was pretty tough to figure out. Had to literally take a look at the mechanize code itself to figure out. Unfortunately, I couldn't test it for sure on an actual form item without a name attribute, though I can do so if you provide the site you're trying to pull, or you can do it yourself.

老实说,表单对象的可用性不是很好.我可以看到的唯一方法是使用 form

The forms objects honestly aren't implemented so well for usability. The only way I can see for you edit a nameless form control's value is by using the form's set_value method:

class HTMLForm:

    # <...>

    set_value(value,
          name=None, type=None, kind=None, id=None, nr=None,
          by_label=False,  # by_label is deprecated
          label=None)

因此,您要在此处设置所需控件的方法是使用nr参数来获取它,并使用表单中控件的索引.不幸的是,您不能使用负整数从后面获取控件,因此要获取最后一种形式,您必须按照nr=len(myform.controls)-1的方式进行操作.

So, what you'd do here to set the control you're looking for is use the nr argument to grab it, using the index of the control in the form. Unfortunately, you can't use negative integers to grab controls from the back, so to grab the last form you'd have to do something along the lines of nr=len(myform.controls)-1.

无论如何,您可以在这里使用该set_value方法,然后按如下所示进行设置:

In any case, what you can then do here is use that set_value method, and you should be set, as such:

forms[2].set_value("LOOK!!!! I SET THE VALUE OF THIS UNNAMED CONTROL!", 
                       nr=5)

,它应该适合您.让我知道怎么回事.

and that should work for you. Let me know how it goes.

这篇关于使用机械化提交不带控件名称的表格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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