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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T03:55:52+00:00 2026-06-04T03:55:52+00:00

I’m trying to parse through a table of rows using beautiful soup and save

  • 0

I’m trying to parse through a table of rows using beautiful soup and save values of each row in a dict.

One hiccup is the structure of the table has some rows as the section headers. So for any row with the class ‘header’ I want to define a variable called “section”. Here’s what I have, but it’s not working because it’s saying [‘class’] TypeError: string indices must be integers

Here’s what I have:

for i in credits.contents:
    if i['class'] == 'header':
        section = i.contents
        DATA_SET[section] = {}
    else:
        DATA_SET[section]['data_point_1'] = i.find('td', {'class' : 'data_point_1'}).find('p').contents
        DATA_SET[section]['data_point_2'] = i.find('td', {'class' : 'data_point_2'}).find('p').contents
        DATA_SET[section]['data_point_3'] = i.find('td', {'class' : 'data_point_3'}).find('p').contents

Example of data:

<table class="credits">
    <tr class="header">
        <th colspan="3"><h1>HEADER NAME</h1></th>
    </tr>
    <tr>
        <td class="data_point_1"><p>DATA</p></td>
        <td class="data_point_2"><p>DATA</p></td>
        <td class="data_point_3"><p>DATA</p></td>
    </tr>
    <tr>
        <td class="data_point_1"><p>DATA</p></td>
        <td class="data_point_2"><p>DATA</p></td>
        <td class="data_point_3"><p>DATA</p></td>
    </tr>
    <tr>
        <td class="data_point_1"><p>DATA</p></td>
        <td class="data_point_2"><p>DATA</p></td>
        <td class="data_point_3"><p>DATA</p></td>
    </tr>
    <tr class="header">
        <th colspan="3"><h1>HEADER NAME</h1></th>
    </tr>
    <tr>
        <td class="data_point_1"><p>DATA</p></td>
        <td class="data_point_2"><p>DATA</p></td>
        <td class="data_point_3"><p>DATA</p></td>
    </tr>
    <tr>
        <td class="data_point_1"><p>DATA</p></td>
        <td class="data_point_2"><p>DATA</p></td>
        <td class="data_point_3"><p>DATA</p></td>
    </tr>
    <tr>
        <td class="data_point_1"><p>DATA</p></td>
        <td class="data_point_2"><p>DATA</p></td>
        <td class="data_point_3"><p>DATA</p></td>
    </tr>
</table>
  • 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-04T03:55:53+00:00Added an answer on June 4, 2026 at 3:55 am

    Here is one solution, with a slight adaptation of your example data so that the result is clearer:

    from BeautifulSoup import BeautifulSoup
    from pprint import pprint
    
    html = '''<body><table class="credits">
        <tr class="header">
            <th colspan="3"><h1>HEADER 1</h1></th>
        </tr>
        <tr>
            <td class="data_point_1"><p>DATA11</p></td>
            <td class="data_point_2"><p>DATA12</p></td>
            <td class="data_point_3"><p>DATA12</p></td>
        </tr>
        <tr>
            <td class="data_point_1"><p>DATA21</p></td>
            <td class="data_point_2"><p>DATA22</p></td>
            <td class="data_point_3"><p>DATA23</p></td>
        </tr>
        <tr>
            <td class="data_point_1"><p>DATA31</p></td>
            <td class="data_point_2"><p>DATA32</p></td>
            <td class="data_point_3"><p>DATA33</p></td>
        </tr>
        <tr class="header">
            <th colspan="3"><h1>HEADER 2</h1></th>
        </tr>
        <tr>
            <td class="data_point_1"><p>DATA11</p></td>
            <td class="data_point_2"><p>DATA12</p></td>
            <td class="data_point_3"><p>DATA13</p></td>
        </tr>
        <tr>
            <td class="data_point_1"><p>DATA21</p></td>
            <td class="data_point_2"><p>DATA22</p></td>
            <td class="data_point_3"><p>DATA23</p></td>
        </tr>
        <tr>
            <td class="data_point_1"><p>DATA31</p></td>
            <td class="data_point_2"><p>DATA32</p></td>
            <td class="data_point_3"><p>DATA33</p></td>
        </tr>
    </table></body>'''
    
    soup = BeautifulSoup(html)
    rows = soup.findAll('tr')
    
    section = ''
    dataset = {}
    for row in rows:
        if row.attrs:
            section = row.text
            dataset[section] = {}
        else:
            cells = row.findAll('td')
            for cell in cells:
                if cell['class'] in dataset[section]:
                    dataset[section][ cell['class'] ].append( cell.text )
                else:
                    dataset[section][ cell['class'] ] = [ cell.text ]
    
    pprint(dataset)
    

    Produces:

    {u'HEADER 1': {u'data_point_1': [u'DATA11', u'DATA21', u'DATA31'],
                   u'data_point_2': [u'DATA12', u'DATA22', u'DATA32'],
                   u'data_point_3': [u'DATA12', u'DATA23', u'DATA33']},
     u'HEADER 2': {u'data_point_1': [u'DATA11', u'DATA21', u'DATA31'],
                   u'data_point_2': [u'DATA12', u'DATA22', u'DATA32'],
                   u'data_point_3': [u'DATA13', u'DATA23', u'DATA33']}}
    

    EDIT ADAPTATION OF YOUR SOLUTION

    Your code is neat and has only a couple of issues. You use contents in places where you shoul duse text or findAll — I repaired that below:

    soup = BeautifulSoup(html)
    credits = soup.find('table')
    
    section = ''
    DATA_SET = {}
    
    for i in credits.findAll('tr'):
        if i.get('class', '') == 'header':
            section = i.text
            DATA_SET[section] = {}
        else:
            DATA_SET[section]['data_point_1'] = i.find('td', {'class' : 'data_point_1'}).find('p').contents
            DATA_SET[section]['data_point_2'] = i.find('td', {'class' : 'data_point_2'}).find('p').contents
            DATA_SET[section]['data_point_3'] = i.find('td', {'class' : 'data_point_3'}).find('p').contents
    
    print DATA_SET
    

    Please note that if successive cells have the same data_point class, then successive rows will replace earlier ones. I suspect this is not an issue in your real dataset, but that is why your code would return this, abbreviated, result:

    {u'HEADER 2': {'data_point_2': [u'DATA32'],
                   'data_point_3': [u'DATA33'],
                   'data_point_1': [u'DATA31']},
     u'HEADER 1': {'data_point_2': [u'DATA32'],
                   'data_point_3': [u'DATA33'],
                   'data_point_1': [u'DATA31']}}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I am reading a book about Javascript and jQuery and using one of the
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function

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.