其他分享
首页 > 其他分享> > 如何机械化添加到选择列表?

如何机械化添加到选择列表?

作者:互联网

我刚刚开始尝试通过机械化提交Web表单.在this webpage上,有一个列表可供选择,MASTER_MODS.可以在MODS中使用按钮add_MODS来选择它们,或者在IT_MODS中使用按钮add_IT_MODS来选择它们(请参见底部的图).在表单中,它看起来像这样(表单底部的代码):

 <<SNIP>>
<SelectControl(MODS=[*--- none selected ---])>
<IgnoreControl(add_MODS=<None>)>
<SelectControl(MASTER_MODS=[])>
<SelectControl(IT_MODS=[*--- none selected ---])>
<IgnoreControl(remove_IT_MODS=<None>)>
<IgnoreControl(add_IT_MODS=<None>)>
<<SNIP>>

所以我想添加到< SelectControl(MODS = [* ---没有选择---])>和< SelectControl(IT_MODS = [* ---未选择---])>.但是,当我尝试直接使用

br.form[ 'MODS'] = ['Acetyl (N-term)']

我收到mechanize._form.ItemNotFoundError:名称为’Acetyl(N-term)’的项目不足

当我尝试

br.form[ 'add_MODS'] = 'Acetyl (N-term)'

我得到ValueError:控件’add_MODS’被忽略,因此是只读的.

如何将项目添加到MODS和IT_MODS?

图和代码

码:

from mechanize import Browser, _http
br = Browser()    
br.set_handle_robots(False)
br.addheaders = [('User-agent', 'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1) Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
url = "http://www.matrixscience.com/cgi/search_form.pl?FORMVER=2&SEARCH=MIS"
br.select_form( 'mainSearch' )
br.open(url)
print br.form

解决方法:

尝试这个?注释中的解释.

from mechanize import Browser, Item
br = Browser()
br.set_handle_robots(False)
br.addheaders = [('User-agent',
                  'Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.9.0.1)'
                  ' Gecko/2008071615 Fedora/3.0.1-1.fc9 Firefox/3.0.1')]
url = 'http://www.matrixscience.com'\
      '/cgi/search_form.pl?FORMVER=2&SEARCH=MIS'
br.open(url)
br.select_form('mainSearch')

# get the actual control object instead of its contents
mods = br.find_control('MODS')
# add an item
item = Item(mods, {"contents": "Acetyl (N-term)", "value": "Acetyl (N-term)"})
# select it. if you don't, it doesn't appear in the output
# this is probably why MASTER_MODS appears empty
item.selected = True
print br['MODS']
# outputs: ['Acetyl (N-term)']

假设这可行,我从the docs的注释中得到了它:

To add items to a list container, instantiate an Item with its control
and attributes:
Note that you are responsible for getting the attributes correct here,
and these are not quite identical to the original HTML, due to
defaulting rules and a few special attributes (e.g. Items that represent
OPTIONs have a special “contents” key in their .attrs dict). In future
there will be an explicitly supported way of using the parsing logic to
add items and controls from HTML strings without knowing these details.
mechanize.Item(cheeses, {"contents": "mascarpone", "value": "mascarpone"})

标签:mechanize,python
来源: https://codeday.me/bug/20191122/2059767.html