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?.
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: