2010-07-07 59 views
0

如果我要得到的東西像這樣與showforms(),我怎麼會得到Value出去了的SOME_CODE輸入框的?從<select>箱返回<options>如清單與Python(機械化/斜紋)

Form name=ttform (#2) 
## ## __Name__________________ __Type___ __ID________ __Value__________________ 
1  NUMBER     select (None)  ['0'] of ['0', '10', '2', '3', '4', ... 
2  SOMEYEAR     select (None)  ['201009'] of ['201009', '201007'] 
3  SOME_CODE    select (None)  ['AR%'] of ['AR%', 'AR01', 'AR02', ' ... 
4  OTHR_CODE    select (None)  ['%'] of ['%', 'AAEC', 'ACIS', 'AEE' ... 

謝謝!!

+0

這不是一個非常明確的問題。 – 2010-07-10 13:52:25

+0

我正在尋找一種方法來提取給定選擇框的值選項作爲列表。像'getOptionValues('NUMBER')'會返回['0','10','2','3','4'...] – Tyler 2010-07-10 20:08:46

回答

0

調試形式,當我使用以下命令:

def getFormControlByLabel(self, form, label): 
    for control in form.controls: 
    self.log.debug("checking control %s dict = %s" % (control,control.__dict__)) 
    if 'items' in control.__dict__: 
     self.log.debug("control %s has items field len %d" % (control, len(control.__dict__['items']))) 
     if len(control.items) > 0: 
      if 'label' in control.items[0].attrs: 
       self.log.debug("control %s has label %s" % (control, control.items[0].attrs['label'])) 
       if control.items[0].attrs['label'] == label: 
       self.log.debug("control %s has label %s" % (control,label)) 
       return control 
    for control in form.controls: 
    try: 
     if control.items[0].attrs['label'] == label: 
      # y.items[0].attrs['label'] 
      self.log.debug("control %s has matching label %s" % (control,label)) 
      return control 
     else: 
      self.log.debug("control %s has label %s" % (control,control.items[0].attrs['label'])) 
    except: 
     pass 
1

這你想要做什麼。我在一個網站上進行了測試,發現上面有你選擇的控制類型:

>>> import twill.commands 
>>> import BeautifulSoup 
>>> import re 
>>> 
>>> a=twill.commands 
>>> a.config("readonly_controls_writeable", 1) 
>>> b = a.get_browser() 
>>> b.set_agent_string("Mozilla/5.0 (Windows; U; Windows NT 5.1; en-GB; rv:1.8.1.14) Gecko/20080404 Firefox/2.0.0.14") 
>>> b.clear_cookies() 
>>> url="http://www.thesitewizard.com/archive/navigation.shtml" 
>>> b.go(url) 
==> at http://www.thesitewizard.com/archive/navigation.shtml 
>>> form=b.get_form("1") 
>>> b.showforms() 

Form #1 
## ## __Name__________________ __Type___ __ID________ __Value__________________ 
1  newurl     select dummymenu [''] of ['', '#', '#', '', '#'] 


Form #2 
## ## __Name__________________ __Type___ __ID________ __Value__________________ 
1  cmd      hidden (None)  _s-xclick 
2 1 submit     image  (None)   
3  encrypted    hidden (None)  -----BEGIN  PKCS7-----MIIHwQYJKoZIhvc ... 


Form #3 
## ## __Name__________________ __Type___ __ID________ __Value__________________ 
1  None      textarea pagelinkcode <a href="http://www.thesitewizard.co ... 


Form #4 
## ## __Name__________________ __Type___ __ID________ __Value__________________ 
1  q      text  searchterms 
2 1 None      submit (None)  Search 


Form #5 
## ## __Name__________________ __Type___ __ID________ __Value__________________ 
1  cmd      hidden (None)  _s-xclick 
2 1 submit     image  (None)   
3  encrypted    hidden (None)  -----BEGIN PKCS7-----MIIHwQYJKoZIhvc ... 

>>> valOpts=[] 
>>> for c in form.controls: 
...  if c.name=="newurl": 
...  if 'items' in c.__dict__: 
...   print "control %s has items field length %s" % (c, len(c.items)) 
...   if len(c.items)>0: 
...    for itm in range(len(c.items)): 
...     valOpts.append(c.items[itm].attrs['value']) 
... 
control <SelectControl(newurl=[*, #, #,(), #])> has items field length 5 
>>> print valOpts 
['', '#', '#', '', '#'] 
>>>