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 6575057
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T15:20:26+00:00 2026-05-25T15:20:26+00:00

I need to parse HTML using Rails and Nokogiri. Here is the HTML: <body>

  • 0

I need to parse HTML using Rails and Nokogiri. Here is the HTML:

<body>
  <div id="mama">
    <div class="test1">text</div>
    <div class="test2">text2</div>
  </div>
  <div id="mama">
    <div class="test1">text</div>
    <div class="test2">text2</div>
  </div>
  <div id="mama">
    <div class="test1">text</div>
    <div class="test2">text2</div>
  </div>
</body>

How I should form loop question? I’ve tried so many times but still getting an error or bad results…
…

doc.xpath('//div[@id='mama']/?or what?').each do |node|
  parse_file.puts text1 
  parse_file.puts text2
  parse_file.puts text1 
  parse_file.puts \n
end

Result should be like

text from first mama
text2 from first mama
text from first mama

text from second mama
and so on...
  • 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-25T15:20:26+00:00Added an answer on May 25, 2026 at 3:20 pm

    First, note that the HTML you posted is syntactically invalid: it is illegal to have more than one element with the same id attribute value. If you have control over your HTML, you should fix this problem.

    Using that same (invalid) HTML, however, Nokogiri still has no trouble:

    require 'nokogiri'
    doc = Nokogiri::HTML(my_html)
    
    doc.css('#mama').each_with_index do |div,i|
      puts "#{div.at_css('.test1').text} from mama ##{i}"
      puts "#{div.at_css('.test2').text} from mama ##{i}"
    end
    
    #=> text from mama #0
    #=> text2 from mama #0
    #=> text from mama #1
    #=> text2 from mama #1
    #=> text from mama #2
    #=> text2 from mama #2
    

    If you wanted to use XPath directly (as Nokogiri does behind the scenes for the CSS) you would do this:

    doc.xpath("//div[@id='mama']").each_with_index do |div,i|
      puts "#{div.at_xpath("./*[@class='test1']").text} from mama ##{i}"
      puts "#{div.at_xpath("./*[@class='test2']").text} from mama ##{i}"
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using mechanize/nokogiri and need to parse out the following HTML string. can
I am using mechanize/nokogiri and need to parse out a HTML with a lot
I need to extract text from an HTML file using C#. I am trying
I have this bit of html that I need to parse though <p class=feature_list>
I'm using BeautifulSoup to parse a HTML page. I need to extract all URLs
I am using python + BeautifulSoup to parse an HTML document. Now I need
I need to parse many html files using php. foreach($url_array as $url){ $file =
I need to browse the DOM tree of a parsed HTML document. I'm using
I need to parse html for a project and looking for a good html
i need to find away to parse html and css layout to be able

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.