TAGS :Viewed: 8 - Published at: a few seconds ago

[ Ruby mechanize script in python ]

I've got this ruby script:

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 }

According to the intro page, the first part is easy, but I don't know how to handle the form.field_with stuff.

Answer 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())