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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T05:53:32+00:00 2026-05-28T05:53:32+00:00

OK I have been trying to parse a html tag which in it contains

  • 0

OK I have been trying to parse a

html tag which in it contains other tags and text

for example

if I had this html (yes I know using <b> and <i> is bad but it makes for a simple example)

<p> <b> 1 </b> Apple <b> 2 </b> <i> Orange </i> <b> 3 </b> Pineapple </p>

It could render something like this

1 Apple 2 Orange 3 Pineapple

How can I get a relation of

{"1": "Apple", "2": "<i> Orange </i>, "3": "Pineapple"}

I have tried using beautifulsoup tag.next but it doesn’t return with tags instead it stops

I have tried using beautifulsoup tag.find(text = True, recursive = False) doesn’t return anything but a \n

I have tried tags.findAll("b")

for i in b:
    print i.text
    print tags.find(i).text

I have looked up parsing tags in tags and nothing really came up fitting some suggest regexes (sounds like trouble) and some said it can’t be done (not really helpful)

I think what I have to find out how to do is get the html between two tags. I tried iterating through .nextSibling bit it eventually gave me a unicode space so can’t continue iterating through.

Anyone have experience with this?

  • 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-28T05:53:33+00:00Added an answer on May 28, 2026 at 5:53 am

    To accumulate elements (tags and text) before and after each <b> tag in <p>:

    #!/usr/bin/env python
    from collections import defaultdict
    from BeautifulSoup import BeautifulSoup
    
    d = defaultdict(list) # data structure to hold the result
    soup = BeautifulSoup(html)
    i = 0
    for el in soup.p.contents:
        if getattr(el, 'name', None) == 'b':
           i += 1  # switch to next <b> element
        else:
           d[i].append(el)
    
    import pprint
    pprint.pprint(dict(d))
    

    It expresses the intent correctly but it is not as readable and efficient as it could be.

    Output

    {0: [u' '],
     1: [u' Apple '],
     2: [u' ', <i> Orange </i>, u' '],
     3: [u' Pineapple ']}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been trying to parse webpages by use of the HTML DOMObject in
I have been trying to parse this ( http://app.calvaryccm.com/mobile/android/v1/devos ) URL using a SAX
I have been trying to use the HTML Agility Pack to parse HTML into
I have been trying to parse Java exceptions that appear in a log for
I have been trying to find a really fast way to parse yyyy-mm-dd [hh:mm:ss]
I have been trying to get around this error for a day now and
I have been trying to load images which are in database(on local phone) .JSON
I've been trying to parse this feed . If you click on that link,
I have been trying to load a html file in a webview in a
I have been trying to do this for months, and months, and months, and

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.