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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T20:28:13+00:00 2026-06-17T20:28:13+00:00

I have following html: <td class=section> <div style=margin-top:2px; margin-bottom:-10px; > <span class=username><a href=user.php?id=xx>xxUsername</a></span> </div>

  • 0

I have following html:

<td class="section">
    <div style="margin-top:2px; margin-bottom:-10px; ">
    <span class="username"><a href="user.php?id=xx">xxUsername</a></span>
    </div>
    <br>
<span class="comment">
A test comment
</span>
</td>

All I want to retrieve xxUsername and comment text within SPAN tag. So far I have done this:

results = soup.findAll("td", {"class" : "section"})

It does fetches ALL html blocks of the pattern I mentioned above. Now I want to retrieve all children value within a single loop? Is it possible? If not then how do I fetch child nodes information?

  • 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-17T20:28:14+00:00Added an answer on June 17, 2026 at 8:28 pm

    You could try something like this. It basically does what you did above – first iterates through all section-classed td‘s and then iterates through all span text within. This prints out the class, just in case you needed to be more restrictive:

    In [1]: from bs4 import BeautifulSoup
    
    In [2]: html = # Your html here
    
    In [3]: soup = BeautifulSoup(html)
    
    In [4]: for td in soup.find_all('td', {'class': 'section'}):
       ...:     for span in td.find_all('span'):
       ...:         print span.attrs['class'], span.text
       ...:         
    ['username'] xxUsername
    ['comment'] 
    A test comment
    

    Or with a more-convoluted-than-necessary one-liner that will store everything back in your list:

    In [5]: results = [span.text for td in soup.find_all('td', {'class': 'section'}) for span in td.find_all('span')]
    
    In [6]: results
    Out[6]: [u'xxUsername', u'\nA test comment\n']
    

    Or on that same theme, a dictionary with the keys being a tuple of the classes and the values being the text itself:

    In [8]: results = dict((tuple(span.attrs['class']), span.text) for td in soup.find_all('td', {'class': 'section'}) for span in td.find_all('span'))
    
    In [9]: results
    Out[9]: {('comment',): u'\nA test comment\n', ('username',): u'xxUsername'}
    

    Assuming this one is bit closer to what you want, I would suggest rewriting as:

    In [10]: results = {}
    
    In [11]: for td in soup.find_all('td', {'class': 'section'}):
       ....:     for span in td.find_all('span'):
       ....:         results[tuple(span.attrs['class'])] = span.text
       ....:         
    
    In [12]: results
    Out[12]: {('comment',): u'\nA test comment\n', ('username',): u'xxUsername'}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following html : <div class=mainmenu> <ul class=blue> <li class=><a href=tst/ id=a1_up><span
I have following HTML: <div class=olympics style=display: block; position: absolute; left: 250px; top: px;>
I have the following HTML: <section id=infopic class=grid_5 prefix_1 style=height: 500px;> <div id=login-image></div> </section>
I'm a jQuery newbie. I've something like the following HTML: <div class=dcsns-youtube> <span class=section-thumb>
I have the following HTML code: <section id=content class=grid_9> <div class=block-content grey-bg> <div class=container_12>
I have the following html tree : <div class=section yellow id=section1> <div id=content-wrap1 class=content-wrap>
I have the following HTML code: <section class=gallery> <a href= id=link-prev>prev</a> <a href= id=link-next>next</a>
I have the following construct in my HTML document: <div class=content> <section class=column></section> <aside
I have the following html <div class=section> <div>header</div> <div> contents <div>sub contents 1</div> <div>sub
I have following HTML <div id=header1 class=toggle>Section 1 : Create an account</div> <div id=section1>

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.