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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T07:27:41+00:00 2026-05-26T07:27:41+00:00

I have HTML like this: <h1> Header is here</h1> <h2>Header 2 is here</h2> <p>

  • 0

I have HTML like this:

<h1> Header is here</h1>
  <h2>Header 2 is here</h2>
     <p> Extract me!</p>
     <p> Extract me too!</p>
  <h2> Next Header 2</h2>
     <p>not interested</p>
     <p>not interested</p>
  <h2>Header 2 is here</h2>
     <p> Extract me!</p>
     <p> Extract me too!</p>

I have a basic Nokogiri CSS node search returning <p> content but I can’t find examples for how to target all text between the Nth closed H2 and the next open H2. I’m creating a CSV with the output so I would also like to read in a file list and put the URL as first result.

  • 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-26T07:27:42+00:00Added an answer on May 26, 2026 at 7:27 am
    require 'rubygems'
    require 'nokogiri'
    
    h = '<h1> Header is here</h1>
      <h2>Header 2 is here</h2>
         <p> Extract me!</p>
         <p> Extract me too!</p>
      <h2> Next Header 2</h2>
         <p>not interested</p>
         <p>not interested</p>
      <h2>Header 2 is here</h2>
         <p> Extract me!</p>
         <p> Extract me too!</p>
    '
    
    doc = Nokogiri::HTML(h)
    
    # Specify the range between delimiter tags that you want to extract
    # triple dot is used to exclude the end point
    # 1...2 means 1 and not 2
    EXTRACT_RANGES = [
      2...3,
      4...5
    ]
    
    # Tags which count as delimiters, not to be extracted
    DELIMITER_TAGS = [
      "h1",
      "h2"
    ]
    
    extracted_text = []
    
    i = 0
    # Change /"html"/"body" to the correct path of the tag which contains this list
    (doc/"html"/"body").children.each do |el|
    
      if (DELIMITER_TAGS.include? el.name)
        i += 1
      else
        extract = false
        EXTRACT_RANGES.each do |cur_range|
          if (cur_range.include? i)
            extract = true
            break
          end
        end
    
        if extract
          s = el.inner_text.strip
          unless s.empty?
            extracted_text << el.inner_text.strip
          end
        end
      end
    
    end
    
    # Print out extracted text (each element's inner text is separated by newlines)
    puts extracted_text.join("\n")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a html like this: <html> <head> <link rel=stylesheet type=text/css media=all href=style.css> </head>
I have HTML like this: <a href=# class=modalInput glow rel=#newsletter >Newsletter!</a> This opens a
So I have Html like this http://trac.edgewall.org/wiki/RecentChanges (I want to create some Flash Track
I have HTML code like this : <div> <a>Link A1</a> <a>Link A2</a> <a>Link A3</a>
I have html code like this <tr class=odd> <td><b>Reason for Termination:</b></td> <td>this has bunch
I have an html like this: <div id=container1> <div class=dragme>drag me</div> </div> <div id=container2>
I have a html like this: The idea is that divs are floated left
I have a problem where I have some html like this <p>There is the
I have a HTML table like this: <table border=1> <tbody> <tr> <td><a href=# class=delete>DELETE
I have a HTML table like this: <table border=1> <tbody> <tr> <td><a href=# class=delete>DELETE

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.