当使用 mechanize 更改表单中项目的状态时,我需要在 Python 中创建一个列表,如下所示:
['2009', '2008', '2007', '2006', '2005', '2004',]
该列表必须以尾随逗号结尾,否则它将不起作用。例如,以下代码有效:
br = mechanize.Browser()
br.select_form(nr=0)
br['ctl03'] = ['2009', '2008', '2007',]
但是,以下代码不起作用:
br = mechanize.Browser()
br.select_form(nr=0)
br['ctl03'] = ['2009', '2008', '2007']
这是我不使用尾随逗号时收到的错误消息:
Traceback (most recent call last):
File "C:/Users/Renato/PycharmProjects/Agrolink/faostat.py", line 43, in <module>
br['ctl03$DesktopThreePanes1$ThreePanes$ctl01$TMyears'] = ['2009', '2008', '2007']
File "C:\Python26\lib\site-packages\mechanize\_form.py", line 2782, in __setitem__
control.value = value
File "C:\Python26\lib\site-packages\mechanize\_form.py", line 1977, in __setattr__
self._set_value(value)
File "C:\Python26\lib\site-packages\mechanize\_form.py", line 1985, in _set_value
raise TypeError("ListControl, must set a sequence")
TypeError: ListControl, must set a sequence
Process finished with exit code 1
好吧,在整个下午处理了这个问题之后,我了解到这是我可以让它做我需要的事情而没有尾随逗号:
mylist = ['2009', '2008', '2007']
br.set_value(mylist, name="ctl03")