Connecting Tech Pros Worldwide Forums | Help | Site Map

Need help translating Ruby code

Newbie
 
Join Date: Feb 2008
Posts: 7
#1: Sep 9 '08
Hi I am new to Ruby from Python and am still learning some stuff. I
have a Ruby program that uses the mechanize library to connect to a Web
site and authenticate with a username and password. The issue I have is
that I need to get this code translated into Python. The Python
mechanize library is not very well documented and I was wondering if
anyone here had any experience using this library in Ruby or Python.
Basically the only tricky part of this program is that it needs to add a
field to the form on the page. This extra field, "__EVENTTARGET", needs
to be added explicitly. I cannot find the Python equivalent to the
WWW::Mechanize::Form::Field.new(nil,nil) method. Thank you in advance.
I hope I explained myself well.

Expand|Select|Wrap|Line Numbers
  1. require 'rubygems'
  2. require 'mechanize'
  3.  
  4. Hpricot.buffer_size = 262144
  5.  
  6. auth = {
  7.   :username => "bugmenot2008@yahoo.com",
  8.   :password => "letmeinalready"
  9. }
  10.  
  11. login_url = "http://www.princetonreview.com/Login3.aspx?uidbadge=" # !>
  12. `&' interpreted as argument prefix
  13. agent  = WWW::Mechanize.new
  14. page = agent.get( login_url )
  15. my_form = page.forms.first
  16.  
  17. my_form['ctl00$MasterMainBodyContent$txtUsername']= auth[:username]
  18. my_form['ctl00$MasterMainBodyContent$txtPassword']= auth[:password]
  19.  
  20.  
  21. target_field = WWW::Mechanize::Form::Field.new(nil,nil)
  22. target_field.name = "__EVENTTARGET"
  23. target_field.value  = "ctl00$MasterMainBodyContent$btnLogin"
  24. my_form.fields << target_field
  25.  
  26. new_page = my_form.submit
  27.  
  28. # examine output to make sure it worked
  29. new_page = agent.get("http://www.princetonreview.com/YourStuff.aspx")
  30.  
  31. pp new_page
  32.  



Newbie
 
Join Date: Mar 2009
Posts: 15
#2: Sep 2 '09

re: Need help translating Ruby code


Quote:
auth = {
:username => "bugmenot2008@yahoo.com",
:password => "letmeinalready"
}
I wonder if thats a real username/password...
Reply


Similar Ruby / Ruby on Rails bytes