Mechanize:TypeError:'NoneType'对象不支持项目分配

时间:2016-06-15 19:57:21

标签: python mechanize

尝试填写表格以获取某个城市的商品平均价格。

basket = br.open('http://www.numbeo.com/cost-of-living/')
read_it = basket.read()

# Select form
for form in br.forms():
    print '%r %r %s' % (form.name, form.attrs.get('id'), form.action)
    for control in form.controls:
        print ' ', control.type, control.name, repr(control.value)

br.form['menu_dispatch_form'] = 'Washington, DC, United States'
br.submit()

我收到了这个错误:

Traceback (most recent call last):
  File "Trip cost calculator/trip costs calculator.py", line 50, in <module>
    br.form['menu_dispatch_form'] = 'Washington, DC, United States'
TypeError: 'NoneType' object does not support item assignment

没有表单名称,但我认为我可以使用表单ID。我发现这个website很有帮助,但与他们的示例不同,我没有text来放入br.form[]。有谁知道发生了什么事?

1 个答案:

答案 0 :(得分:1)

你做错了。您需要选择form,然后才能为表格控件指定值。

你有menu_dispatch_form吗?然后使用br.select_form("menu_dispatch_form")选择表单。要向表单添加数据,您应该为表单控件分配值。 Thisthis可以帮助您查找和分配值以形成控件。