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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T03:33:48+00:00 2026-06-01T03:33:48+00:00

Here is the my python code using BeautifulSoup. The main issue is with the

  • 0

Here is the my python code using BeautifulSoup. The main issue is with the attributes. What I am looking for is, each element of the th should be separated but for some reason it keep generating inside only one individual tag.

from BeautifulSoup import BeautifulSoup, Tag
soup=BeautifulSoup()
mem_attr=['Description','PhysicalID','Slot','Size','Width']
tag1 = Tag(soup, "html")
tag2 = Tag(soup, "table")
tag3 = Tag(soup, "tr")
tag4 = Tag(soup, "th")
tag5 = Tag(soup, "td")
soup.insert(0, tag1)
tag1.insert(0, tag2)
tag2.insert(0, tag3)
for i in range(0,len(mem_attr)):
        tag3.insert(0,tag4)
        tag4.insert(i,mem_attr[i])

print soup.prettify()

Here is its output:

<html>
 <table>
  <tr>
   <th>
    Description
    PhysicalID
    Slot
    Size
    Width
   </th>
  </tr>
 </table>
</html>

What I am looking for is this one.

<html>
     <table>
      <tr>
       <th>
        Description
       </th>
       <th>
        PhysicalID
       </th>
       <th>
        Slot
       </th>
       <th>
        Size
       </th>
       <th>
        Width
       </th>
      </tr>
     </table>
    </html>

Can anyone tell me what is missing in the code?.

  • 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-01T03:33:50+00:00Added an answer on June 1, 2026 at 3:33 am

    You’re putting it in the same th. You never told it to create more than one.

    Here is code more like what you are wanting:

    from BeautifulSoup import BeautifulSoup, Tag
    soup = BeautifulSoup()
    mem_attr = ['Description', 'PhysicalID', 'Slot', 'Size', 'Width']
    html = Tag(soup, "html")
    table = Tag(soup, "table")
    tr = Tag(soup, "tr")
    soup.append(html)
    html.append(table)
    table.append(tr)
    for attr in mem_attr:
        th = Tag(soup, "th")
        tr.append(th)
        th.append(attr)
    
    print soup.prettify()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm building a python application from some source code I've found Here I've managed
So, I know some Python but I thought that I should try to add
I have some code to interface Python to C++ which works fine but every
I'm trying to call some python code using Py_CompileString() and PyEval_EvalCode() . It works
I'm using uwsgi on Nginx to run some Python code. I'd like to bind
I want to insert data in MYSQL database using python here my code import
I'm using boost::python to embed some python code into an app. I was able
I'm writing a Python module for some C++ code using SIP. However whilst I
I have some python code for Google App Engine that responds with the string
I am using Python code which was written one year ago. My OS 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.