2011-12-18 85 views
0

我有了這個Ruby腳本:的Ruby腳本機械化在Python

require 'mechanize' 
agent = Mechanize.new 
page = agent.get "http://www.speech.sri.com/projects/srilm/download.html" 
form = page.forms.last 
form.field_with(name: "name").value = "Hans Meier" 
form.field_with(name: "org").value = "Meier AG" 
form.field_with(name: "email").value = "hans.meier.org" 
form.field_with(name: "address").value = "Baslerstrasse 32" 
form.field_with(name: "file").value = "/project/srilm/srilm-1.6.0.tar.gz" 
File.open('srilm.tgz') {|f| f.write agent.submit(form).body } 

按照intro page,第一部分是容易的,但我不知道如何處理form.field_with東西。

回答

1
import mechanize 
br = mechanize.Browser() 
br.open("http://www.speech.sri.com/projects/srilm/download.html") 
br.select_form(nr = True) 
br['name'] = 'Hans Meier' 
br['org'] = 'Meier AG' 
br['email'] = 'hans.meier.org' 
br['address'] = 'Baslerstrasse 32' 
br['file'] = ['/project/srilm/srilm-1.6.0.tar.gz'] 
form_submission = br.submit() 
with open('srilm.tgz', 'w') as f: 
    f.write(form_submission.read()) 
+0

從技術上講,這不等同於'field_with'(它允許多條件或正則表達式匹配),但是OP並沒有真正需要在Ruby腳本中使用它。 – 2011-12-18 21:04:32