Need help translating Ruby code

Discussion in 'Ruby' started by Kevin Tran, Sep 9, 2008.

  1. Kevin Tran

    Kevin Tran Guest

    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.




    Code:
    require 'rubygems'
    require 'mechanize'
    
    Hpricot.buffer_size = 262144
    
    auth = {
    :username => "",
    :password => "letmeinalready"
    }
    
    login_url = "http://www.princetonreview.com/Login3.aspx?uidbadge=" # !>
    `&' interpreted as argument prefix
    agent  = WWW::Mechanize.new
    page = agent.get( login_url )
    my_form = page.forms.first
    
    my_form['ctl00$MasterMainBodyContent$txtUsername']= auth[:username]
    my_form['ctl00$MasterMainBodyContent$txtPassword']= auth[:password]
    
    
    target_field = WWW::Mechanize::Form::Field.new(nil,nil)
    target_field.name = "__EVENTTARGET"
    target_field.value  = "ctl00$MasterMainBodyContent$btnLogin"
    my_form.fields << target_field
    
    new_page = my_form.submit
    
    # examine output to make sure it worked
    new_page = agent.get("http://www.princetonreview.com/YourStuff.aspx")
    
    pp new_page
    
    --
    Posted via http://www.ruby-forum.com/.
    Kevin Tran, Sep 9, 2008
    #1
    1. Advertising

  2. Kevin Tran

    Glen Holcomb Guest

    [Note: parts of this message were removed to make it a legal post.]

    On Mon, Sep 8, 2008 at 9:28 PM, Kevin Tran <> wrote:

    > 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.
    >
    >
    >
    >
    >
    Code:
    > require 'rubygems'
    > require 'mechanize'
    >
    > Hpricot.buffer_size = 262144
    >
    > auth = {
    >  :username => "",
    >  :password => "letmeinalready"
    > }
    >
    > login_url = "http://www.princetonreview.com/Login3.aspx?uidbadge=" # !>
    > `&' interpreted as argument prefix
    > agent  = WWW::Mechanize.new
    > page = agent.get( login_url )
    > my_form = page.forms.first
    >
    > my_form['ctl00$MasterMainBodyContent$txtUsername']= auth[:username]
    > my_form['ctl00$MasterMainBodyContent$txtPassword']= auth[:password]
    >
    >
    > target_field = WWW::Mechanize::Form::Field.new(nil,nil)
    > target_field.name = "__EVENTTARGET"
    > target_field.value  = "ctl00$MasterMainBodyContent$btnLogin"
    > my_form.fields << target_field
    >
    > new_page = my_form.submit
    >
    > # examine output to make sure it worked
    > new_page = agent.get("http://www.princetonreview.com/YourStuff.aspx")
    >
    > pp new_page
    > 
    > --
    > Posted via http://www.ruby-forum.com/.
    >
    >

    I think this would be a better question for a Python list as you want a
    Python answer.

    --
    "Hey brother Christian with your high and mighty errand, Your actions speak
    so loud, I can't hear a word you're saying."

    -Greg Graffin (Bad Religion)
    Glen Holcomb, Sep 9, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Eirik Eldorsen

    need help translating VB.NET -> C#

    Eirik Eldorsen, Nov 25, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    533
    =?Utf-8?B?d2VpY2h1bmcgW01DU0QsIE1DREJBXQ==?=
    Nov 26, 2004
  2. Davis Marques
    Replies:
    6
    Views:
    310
    Paul Rubin
    Jan 12, 2004
  3. lombardm

    Translating c code help required

    lombardm, Sep 26, 2007, in forum: C Programming
    Replies:
    9
    Views:
    350
    lombardm
    Sep 27, 2007
  4. Sam Dela Cruz
    Replies:
    37
    Views:
    394
  5. Kamaljeet Saini
    Replies:
    2
    Views:
    148
    Kamaljeet Saini
    Jan 30, 2009
Loading...

Share This Page