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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T14:43:44+00:00 2026-06-17T14:43:44+00:00

I have a simple html file like this. In fact I pulled it from

  • 0

I have a simple html file like this. In fact I pulled it from a wiki page, removed some html attributes and converted to this simple html page.

<html>
   <body>
      <h1>draw electronics schematics</h1>
      <h2>first header</h2>
      <p>
         <!-- ..some text images -->
      </p>
      <h3>some header</h3>
      <p>
         <!-- ..some image -->
      </p>
      <p>
         <!-- ..some text -->
      </p>
      <h2>second header</h2>
      <p>
         <!-- ..again some text and images -->
      </p>
   </body>
</html>

I read this html file using python and beautiful soup like this.

from bs4 import BeautifulSoup

soup = BeautifulSoup(open("test.html"))

pages = []

What I’d like to do is split this html page into two parts. The first part will be between first header and second header. And the second part will be between second header <h2> and </body> tags. Then I’d like to store them in a list eg. pages. So I’d be able to create multiple pages from an html page according to <h2> tags.

Any ideas on how should I do this? Thanks..

  • 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-06-17T14:43:46+00:00Added an answer on June 17, 2026 at 2:43 pm

    Look for the h2 tags, then use .next_sibling to grab everything until it’s another h2 tag:

    soup = BeautifulSoup(open("test.html"))
    pages = []
    h2tags = soup.find_all('h2')
    
    def next_element(elem):
        while elem is not None:
            # Find next element, skip NavigableString objects
            elem = elem.next_sibling
            if hasattr(elem, 'name'):
                return elem
    
    for h2tag in h2tags:
        page = [str(h2tag)]
        elem = next_element(h2tag)
        while elem and elem.name != 'h2':
            page.append(str(elem))
            elem = next_element(elem)
        pages.append('\n'.join(page))
    

    Using your sample, this gives:

    >>> pages
    ['<h2>first header</h2>\n<p>\n<!-- ..some text images -->\n</p>\n<h3>some header</h3>\n<p>\n<!-- ..some image -->\n</p>\n<p>\n<!-- ..some text -->\n</p>', '<h2>second header</h2>\n<p>\n<!-- ..again some text and images -->\n</p>']
    >>> print pages[0]
    <h2>first header</h2>
    <p>
    <!-- ..some text images -->
    </p>
    <h3>some header</h3>
    <p>
    <!-- ..some image -->
    </p>
    <p>
    <!-- ..some text -->
    </p>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have simple HTML page with svg like this: <div id=plan> <svg xmlns=http://www.w3.org/2000/svg version=1.1>
I have this simple piece of HTML code: <div> <input type=file name=english-file /> </div>
I have simple HTML code with some JavaScript. It looks like: <html> <head> <script
So I have this simple html file <!DOCTYPE HTML> <html manifest=cache.manifest><head> <meta name=apple-mobile-web-app-capable content=yes>
Okay this seems like a real noob question. I currently have a simple html
I have a simple HTML file, that gets data from the server and outputs
I have a simple XML file that looks like this: <?xml version=1.0 ?> <devices>
I want to have a simple HTML file act as a bootstrap for a
can anyone help? I have a simple html file which i am filling via
I have these (sample) lines in a HTML-file: test.ABC.test test.ABCD.test test.ABCE.test And this Ant

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.