机械化如何添加到选择列表? [英] Mechanize how to add to a select list?

查看:57
本文介绍了机械化如何添加到选择列表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚刚开始尝试通过机械化提交Web表单.在此网页上,有一个要列出的项目列表从MASTER_MODS中选择.可以使用按钮add_MODSMODS中或使用按钮add_IT_MODSIT_MODS中进行选择(请参见底部的图).在表单中,它看起来像这样(表单底部的代码):

I just started experimenting with submitting webforms through mechanize. On this webpage there is a list of items to select from, MASTER_MODS. These can be selected in either MODS using a butten add_MODS or in IT_MODS using a button add_IT_MODS (see figure at the bottom). In the form it looks like this (code for form at bottom):

 <<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=[*--- none selected ---])><SelectControl(IT_MODS=[*--- none selected ---])>.但是,当我尝试直接使用

So I want to add to <SelectControl(MODS=[*--- none selected ---])> and <SelectControl(IT_MODS=[*--- none selected ---])>. However, when I try to add an item directly with

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

我得到mechanize._form.ItemNotFoundError: insufficient items with name 'Acetyl (N-term)'

当我尝试

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

我得到ValueError: control 'add_MODS' is ignored, hence read-only.

如何向MODSIT_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

推荐答案

尝试一下?在注释中进行解释.

Try this? Explanation in the comments.

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)']

假设这可行,我从文档中的注释中获得了

Assuming this works, I got it from the comments in the docs:

要将项目添加到列表容器,请使用其控件实例化一个Item 和属性: 请注意,您有责任确保此处的属性正确无误, 这些与原始HTML不太相同,原因是 默认规则和一些特殊属性(例如,代表 选项在其.attrs字典中具有特殊的内容"键).在未来 将会有一种明确支持的使用解析逻辑的方式 在不知道这些详细信息的情况下从HTML字符串添加项目和控件. mechanize.Item(cheeses, {"contents": "mascarpone", "value": "mascarpone"})

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"})

这篇关于机械化如何添加到选择列表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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