Python,mechanize,尝试处理select和checkbox控件
我正在尝试用mechanize填充一个相当复杂的web表单,结果出现了一个错误。 我做了以下工作:Python,mechanize,尝试处理select和checkbox控件,python,html,webforms,mechanize,Python,Html,Webforms,Mechanize,我正在尝试用mechanize填充一个相当复杂的web表单,结果出现了一个错误。 我做了以下工作: from mechanize import Browser url = 'some_url_here' br = Browser() # going around robot.txt br.set_handle_robots(False) br.set_handle_equiv(False) br.addheaders = [('User-agent', 'Mozilla/5.0 (X11;
from mechanize import Browser
url = 'some_url_here'
br = Browser() # going around robot.txt
br.set_handle_robots(False)
br.set_handle_equiv(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')]
br.open(url) # opening the website I need
# here I did some work studying html and finding the forms I need
br.select_form(nr=0) # I need the first form and here the controls I need
print br.form.controls[2].name, ' type=', br.form.controls[2].type
print br.form.controls[4].name, ' type=', br.form.controls[4].type
print br.form.controls[6].name, ' type=', br.form.controls[6].type
print br.form.controls[7].name, ' type=', br.form.controls[7].type
print br.form.controls[9].name, ' type=', br.form.controls[9].type
out:
okato type= select
grvdtov type= select
viddata type= select
god_chk type= checkbox
period_chk type= checkbox
# and here where I'm trying to select what I need and fail:
br.select_form(nr=0)
br.form.controls[2] = ['Российская Федерация'] # selecting values I need
br.form.controls[4] = ['Все товары и услуги', 'Базовый индекс потребительских цен']
br.form.controls[6] = ['в % к предыдущему месяцу']
br.controls[7].items[0].selected=True # ticking the checkbox
br.controls[9].items[0].selected=True
reponse = br.submit() # submitting
这里是我得到的错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-70-c173bbf420d3> in <module>()
5 br.controls[7].items[0].selected=True
6 br.controls[9].items[0].selected=True
----> 7 reponse = br.submit()
C:\Users\Asus\Anaconda3\envs\Python2\lib\site-packages\mechanize\_mechanize.pyc in submit(self, *args, **kwds)
539
540 """
--> 541 return self.open(self.click(*args, **kwds))
542
543 def click_link(self, link=None, **kwds):
C:\Users\Asus\Anaconda3\envs\Python2\lib\site-packages\mechanize\_mechanize.pyc in click(self, *args, **kwds)
528 if not self.viewing_html():
529 raise BrowserStateError("not viewing HTML")
--> 530 request = self.form.click(*args, **kwds)
531 return self._add_referer_header(request)
532
C:\Users\Asus\Anaconda3\envs\Python2\lib\site-packages\mechanize\_form.pyc in click(self, name, type, id, nr, coord, request_class, label)
2997 """
2998 return self._click(name, type, id, label, nr, coord, "request",
-> 2999 self._request_class)
3000
3001 def click_request_data(self,
C:\Users\Asus\Anaconda3\envs\Python2\lib\site-packages\mechanize\_form.pyc in _click(self, name, type, id, label, nr, coord, return_type, request_class)
3190 try:
3191 control = self._find_control(
-> 3192 name, type, "clickable", id, label, None, nr)
3193 except ControlNotFoundError:
3194 if ((name is not None) or (type is not None) or (id is not None) or
C:\Users\Asus\Anaconda3\envs\Python2\lib\site-packages\mechanize\_form.pyc in _find_control(self, name, type, kind, id, label, predicate, nr)
3144 if type is not None and type != control.type:
3145 continue
-> 3146 if kind is not None and not control.is_of_kind(kind):
3147 continue
3148 if id is not None and id != control.id:
AttributeError: 'list' object has no attribute 'is_of_kind'
---------------------------------------------------------------------------
AttributeError回溯(最近一次呼叫上次)
在()
5 br.控件[7]。项[0]。选定项=True
6 br.控件[9]。项[0]。选定项=True
---->7响应=br.提交()
提交中的C:\Users\Asus\Anaconda3\envs\Python2\lib\site packages\mechanize\\u mechanize.pyc(self,*args,**kwds)
539
540 """
-->541返回self.open(self.click(*args,**kwds))
542
543 def点击链接(self,link=None,**kwds):
C:\Users\Asus\Anaconda3\envs\Python2\lib\site packages\mechanize\\u mechanize.pyc单击(self,*args,**kwds)
528如果不是self.viewing_html():
529 raise BrowserStateError(“不查看HTML”)
-->530 request=self.form.click(*args,**kwds)
531返回self.\u添加\u引用\u头(请求)
532
点击C:\Users\Asus\Anaconda3\envs\Python2\lib\site packages\mechanize\\ form.pyc(self、name、type、id、nr、coord、request\u class、label)
2997 """
2998返回自我。\单击(名称、类型、id、标签、编号、坐标,“请求”,
->2999自我要求(课程)
3000
3001 def点击请求数据(自身、,
C:\Users\Asus\Anaconda3\envs\Python2\lib\site packages\mechanize\ form.pyc in\单击(self、name、type、id、label、nr、coord、return\ type、request\ class)
3190试试:
3191控制=自我。\找到\u控制(
->3192名称,类型,“可点击”,id,标签,无,编号)
3193除ControlNotFoundError外:
3194如果((名称不是None)或(类型不是None)或(id不是None)或
C:\Users\Asus\Anaconda3\envs\Python2\lib\site packages\mechanize\ form.pyc在\u find\控件中(self、name、type、kind、id、label、predicate、nr)
3144如果类型不是无且类型!=control.type:
3145继续
->3146如果种类不是无且不是控制。是否为种类(种类):
3147继续
3148如果id不是None且id!=control.id:
AttributeError:“列表”对象没有属性“属于”
有谁能告诉我哪里出了问题,以及如何解决?
谢谢大家!