2013-05-28 51 views
0

我寫了這個代碼,從Excel文件中讀取數據,並應在網站上提交的數據的一種形式:的urllib2如何發佈包含多個提交按鈕

import xlrd,urllib2,urllib 
book = xlrd.open_workbook("C:\\Users\\file.xls") 
sh = book.sheet_by_index(0) 
url = 'https://myurl' 

for rx in range(sh.nrows): 
    riga=sh.row(rx) 
    denominazione=riga[0].value 
    descrizione=riga[1].value 
    start=str(riga[2].value)[0:4] 
    end=str(riga[3].value)[0:4] 
    area=riga[4].value[0:2] 
    form_data = {'denominazione': '%s' % (denominazione.encode('utf-8')), 
       'descrizione': '%s' % (descrizione.encode('utf-8')), 
       'categoria': '5', 
       'contratto':'n', 
       'periodo_dal':'%s' % start, 
       'periodo_al': '%s' % end, 
       'area_1_area':'%s' % area, 
       'area_1_perc':'100%', 
       '@action': 'Save all' 
       } 
    params = urllib.urlencode(form_data) 
    print params 
    response = urllib2.urlopen(url, params) 
    print response.msg 
    data = response.readlines() 

    fd= open("C:\\Users\\output.html","w") 
    for idx,item in enumerate(data): 
     fd.write(item) 
    fd.close() 

但它失敗,我想這是因爲網站的表格有幾個提交按鈕。正如你從我的源頭可以看到的,我嘗試編碼POST操作來調用:'@action':'全部保存',但即使這樣也行不通。我如何用固定操作設置此POST請求?

回答

1

找到了!

對不起,我剛剛找到答案,我的問題。

要做到這一點我有編碼行動,這樣一個平常PARAM:

(X)HTML: 
<input type="submit" class="ult_btn01" value="Save" name="save" /> 
<input type="submit" class="ult_btn01" value="Save all" name="save_all" /> 

python: 
form_data = {'denominazione': '%s' % (denominazione.encode('utf-8')), 
       'descrizione': '%s' % (descrizione.encode('utf-8')), 
       'categoria': '5', 
       'contratto':'n', 
       'periodo_dal':'%s' % start, 
       'periodo_al': '%s' % end, 
       'area_1_area':'%s' % area, 
       'area_1_perc':'100', 
       'save_all': 'Save all' 
      }