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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:03:49+00:00 2026-06-17T22:03:49+00:00

This is a pretty small question that has been almost resolved in a previous

  • 0

This is a pretty small question that has been almost resolved in a previous question.

Problem is that right now i have and array of comments, but it does not quite what I need. I get an array of comments-content. And I need to get the html in-between.

Say I have something like:

<p>some html here<p>
<!-- begin mark -->
<p>Html i'm interested at.</p>
<p>More html i want to pull out of the document.</p>
<!-- end mark -->
<!-- begin mark -->
<p>This will be pulled later, but we will come to it when I get to pull the previous section.</p>
<!-- end mark -->

In a reply, they point to Crummy explanation on navigating the html tree, but I didnt find there and answer to my problem.

Any ideas? Thanks.

PS. Extra kudos if someone point me an elegant way to repeat the process a few times in a document, as I probably may get it to work, but poorly 😀

Edited to add:

With the information provided by Martijn Pieters, I got to pass the comments array obtained using the above code to the generator function he designed. So this gives no error:

for elem in comments:
    htmlcode = allnext(comments)
    print htmlcode

I think now it will be possible to manipulate the htmlcode content before iterating through the array.

  • 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-17T22:03:50+00:00Added an answer on June 17, 2026 at 10:03 pm

    You can use the .next_sibling pointer to get to the next element. You can use that to find everything following a comment, up to but not including another comment:

    from bs4 import Comment
    
    def allnext(comment):
        curr = comment
        while True:
            curr = curr.next_sibling
            if isinstance(curr, Comment):
                return
            yield curr
    

    This is a generator function, you use it to iterate over all ‘next’ elements:

    for elem in allnext(comment):
        print elem
    

    or you can use it to create a list of all next elements:

    elems = list(allnext(comment))
    

    Your example is a little too small for BeautifulSoup and it’ll wrap each comment in a <p> tag but if we use a snippet from your original target www.gamespot.com this works just fine:

    <div class="ad_wrap ad_wrap_dart"><div style="text-align:center;"><img alt="Advertisement" src="http://ads.com.com/Ads/common/advertisement.gif" style="display:block;height:10px;width:120px;margin:0 auto;"/></div>
    <!-- start of gamespot gpt ad tag -->
    <div id="div-gpt-ad-1359295192-lb-top">
    <script type="text/javascript">
            googletag.display('div-gpt-ad-1359295192-lb-top');
        </script>
    <noscript>
    <a href="http://pubads.g.doubleclick.net/gampad/jump?iu=/6975/row/gamespot.com/home&amp;sz=728x90|970x66|970x150|970x250|960x150&amp;t=pos%3Dtop%26platform%3Ddesktop%26&amp;c=1359295192">
    <img src="http://pubads.g.doubleclick.net/gampad/ad?iu=/6975/row/gamespot.com/home&amp;sz=728x90|970x66|970x150|970x250|960x150&amp;t=pos%3Dtop%26platform%3Ddesktop%26&amp;c=1359295192"/>
    </a>
    </noscript>
    </div>
    <!-- end of gamespot gpt tag -->
    </div>
    

    If comment is a reference to the first comment in that snippet, the allnext() generator gives me:

    >>> list(allnext(comment))
    [u'\n', <div id="div-gpt-ad-1359295192-lb-top">
    <script type="text/javascript">
            googletag.display('div-gpt-ad-1359295192-lb-top');
        </script>
    <noscript>
    <a href="http://pubads.g.doubleclick.net/gampad/jump?iu=/6975/row/gamespot.com/home&amp;sz=728x90|970x66|970x150|970x250|960x150&amp;t=pos%3Dtop%26platform%3Ddesktop%26&amp;c=1359295192">
    <img src="http://pubads.g.doubleclick.net/gampad/ad?iu=/6975/row/gamespot.com/home&amp;sz=728x90|970x66|970x150|970x250|960x150&amp;t=pos%3Dtop%26platform%3Ddesktop%26&amp;c=1359295192"/>
    </a>
    </noscript>
    </div>, u'\n']
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is a pretty esoteric issue that I can't produce a small test case
This is pretty much the same problem i have, except with very different code:
I have an iphone app that has been submitted that makes a lot of
I have a server hosting a website of mine that has almost zero-traffic. A
I have this small application that I'm building as an exercise to learn the
First off, apologies if this question has been asked before but I couldn't find
I have a rather small but weird problem that I can't seem to fix.
this pretty much is the thing. When in tinymce I press anything that is
I have written something like this pretty easily in C# ( string GetUrl(new {
This is pretty straight forward. EDIT: Updated question and added fourth echo. Here is

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.