python - Mechanize :类型错误: 'NoneType' 对象不支持项目分配

标签 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[] .有谁知道这是怎么回事?

最佳答案

你这样做是错误的。您需要选择一个 form ,只有这样您才能为表单控件赋值​​。

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

关于python - Mechanize :类型错误: 'NoneType' 对象不支持项目分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37844626/

相关文章:

python - Sphinx autodoc 使用模拟卡在 random.choice() 上

python - USB : usb_device_handle_win. cc:1020 在 Windows10 上使用 Selenium 的 ChromeDriver v87/Chrome v87 无法从节点连接错误中读取描述符

python - 如何在Jupyter笔记本中转到函数的定义?

python - 我的 python 程序总是在运行几个小时后断开互联网连接,如何调试和解决这个问题?

python Mechanize 奇数 .read() 输出

python - 在 Python 中集成离散点

python - Django 1.6 Formset 的 bug : "referenced before assignment"

Python、Mechanize - 即使在 set_handle_robots 和 add_headers 之后,robots.txt 也不允许请求

python - 如何在 Python ClientForm 中设置一个不存在的字段?

ruby-on-rails - ruby regex 使用最后一个匹配来分隔字符串,但应该首先使用