Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6115923
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T15:08:52+00:00 2026-05-23T15:08:52+00:00

I need to scrape data from a site, but it requires my login first.

  • 0

I need to scrape data from a site, but it requires my login first. I’ve been using hpricot to successfully scrape other sites, but I’m new to using mechanize, and I’m truly baffled by how to work it.

I see this example commonly quoted:

require 'rubygems'
require 'mechanize'

a = Mechanize.new
a.get('http://rubyforge.org/') do |page|
  # Click the login link
  login_page = a.click(page.link_with(:text => /Log In/))

  # Submit the login form
  my_page = login_page.form_with(:action => '/account/login.php') do |f|
    f.form_loginname  = ARGV[0]
    f.form_pw         = ARGV[1]
  end.click_button

  my_page.links.each do |link|
    text = link.text.strip
    next unless text.length > 0
    puts text
  end
end

But I’ve found it extremely cryptic. The part I don’t understand in particular is what’s going on here:

f.form_loginname  = ARGV[0]
f.form_pw         = ARGV[1]

How have those input tags from the page suddenly become methods? Am I missing something here? When I try to recreate it, to login to AppDataPro (http://www.appdata.com/login) I run into the problem that the input name contains brackets, like this:

<Table> 
<tr><td width="150"> 
   <label for="user_session_username">Username</label><br /> 
</td><td > 
    <input id="user_session_username" name="user_session[username]" size="30" type="text" /> 
</td></tr> 
<tr><td> 
   <label for="user_session_password">Password</label><br /> 
</td><td> 
    <input id="user_session_password" name="user_session[password]" size="30" type="password" /> 
</td></tr> 
</table> 

This is my attempt to use mechanize:

    a = Mechanize.new
    a.get('http://www.appdata.com/login') do |page|
        # Click the login link
        login_page = a.click(page.link_with(:text => /Login/)) #login_page is basically a doc of appdata/login

        my_page = login_page.form_with(:action => '/login') do |f|
            f.user_session[username] =  '****username here?****'
            f.user_session[password] =  '****password here?****'
        end

    end

but it causes the error,

logintest01.rb:21:in `block (2 levels) in <main>': undefined method `user_session' for nil:NilClass (NoMethodError)

What’s wrong with what I’m doing?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T15:08:53+00:00Added an answer on May 23, 2026 at 3:08 pm

    This is the approach I usually take. It hasn’t failed me:

    username_field = form.field_with(:name => "user_session[username]")
    username_field.value = "whatever_user"
    password_field = form.field_with(:name => "user_session[password]")
    password_field.value = "whatever_pwd"
    form.submit
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need to scrape some data from webpages. But I have some encoding problems
I am using BeautifulSoup to scrape data from a webpage. I want to compare
I am trying to scrape the datas from a webpage, but I get need
I need to scrape from a website that requires authentication, that is a user
Hey, I'd like to scrape some data from my blog using YQL: SELECT *
I need to scrape Form 10-K reports (i.e. annual reports of US companies) from
i need to scrape (with approval) web sites before I start to write my
I am trying to scrape some data from a page with a table based
I have this situation where I need to get some data from a webpage
I want to scrape string data from some binary text files that contain embedded

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.