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

  • Home
  • SEARCH
  • 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 5971597
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T20:32:18+00:00 2026-05-22T20:32:18+00:00

i want to get the 10 google search results links (href) using mechanize so

  • 0

i want to get the 10 google search results links (href) using mechanize so i wrote this code, but the code does not return the right google search results, what should i write?

    @searchword = params[:q]
    @sitesurl = Array.new
    agent = Mechanize.new
    page = agent.get("http://www.google.com")
    search_form = page.form_with(:name => "f")
    search_form.field_with(:name => "q").value = @searchword.to_s 
    search_results = agent.submit(search_form)
    count = 0
    c = 0
    while  c < 10
    if (search_results/"li")[count].attributes['class'].to_s == "g knavi"
      site = (search_results/"li")[count]
      code = (site/"a")[0].attributes['href']
      @sitesurl << code
      c += 1
    end
    count += 1
end
  • 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-22T20:32:18+00:00Added an answer on May 22, 2026 at 8:32 pm

    Something like this should work:

    @searchword = params[:q]
    @sitesurl = Array.new
    agent = Mechanize.new
    page = agent.get("http://www.google.com")
    search_form = page.form_with(:name => "f")
    search_form.field_with(:name => "q").value = @searchword.to_s     
    search_results = agent.submit(search_form)
    
    (search_results/"li.g").each do |result|
      @sitesurl << (result/"a").first.attribute('href') if result.attribute('class').to_s == 'g knavi'
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to parse Google search and get links to RSS from each item
i want to get the google results as div. but i take error Object
I want to use the Camera Image to get the google search results. I
I want to get html tags.For ex www.google.com. When I enter this site with
I want to get first and last name of a google account using gdata
In my application i want to get the weather report I'm using http://www.google.com/ig/api?weather= for
I have two issues in this google chart. I want to either get rid
I want to get all the search results for a particular keyword search on
Hey i want to list the results from a google search, i saw that
String str is set to: {query:{count:1,created:2012-02-26T21:38:27Z,lang:en-US,results:{title:Google Search}}} I want to use Regex to get

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.