Beautifulsoup add attribute to first <td> item in a table

I would like to get a table html code from a website with Beautifulsoup and I need to add attribute to the first td item. I have:

try:
        description=hun.select('#description > div.tab-pane-body > div > div > div > table')[0]
        description+="<style type=text/css>td:first-child { font-weight: bold; width: 5%; } td:nth-child(2) { width: 380px } td:nth-child(3) { font-weight: bold; }</style>"
    except:
        description=None

The selected description‘s code:

<table border="0" cellpadding="0" cellspacing="0" width="704">
    <tbody>
        <tr>
            <td valign="top" width="704" style="">
                <p><span>Short description </span></p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="123" style="">
                <p><span>Additional data</span></p>
            </td>
        </tr>
    </tbody>
</table>

I would like to add a colspan attribute to the first <td> and keep changes in the description variable:

<table border="0" cellpadding="0" cellspacing="0" width="704">
    <tbody>
        <tr>
            <td valign="top" width="704" style="" colspan="4">
                <p><span>Short description </span></p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="123" style="">
                <p><span>Additional data</span></p>
            </td>
        </tr>
    </tbody>
</table>

I tried:

hun=BeautifulSoup(f,'html.parser')    

try:
    description2=hun.select('#description > div.tab-pane-body > div > div > div > table')[0]
    description2+="<style type=text/css>td:first-child { font-weight: bold; width: 5%; } td:nth-child(2) { width: 380px } td:nth-child(3) { font-weight: bold; }</style>"
    
    soup = BeautifulSoup(description2, 'html.parser')
    description = soup.td['colspan'] = 4

…but it is not working, the output is "4", instead of the table’s html code with attribute added.

I found it, it must be like this:

hun=BeautifulSoup(f,'html.parser')    
    
    try:
        description2=hun.select('#description > div.tab-pane-body > div > div > div > table')[0]
        description2+="<style type=text/css>td:first-child { font-weight: bold; width: 5%; } td:nth-child(2) { width: 380px } td:nth-child(3) { font-weight: bold; }</style>"
        
        soup = BeautifulSoup(description2, 'html.parser')
        soup.td['colspan'] = 4
        description = soup

>Solution :

Just select the first <td> and add attribute colspan:

from bs4 import BeautifulSoup

html_doc = '''\
<table border="0" cellpadding="0" cellspacing="0" width="704">
    <tbody>
        <tr>
            <td valign="top" width="704" style="">
                <p><span>Short description </span></p>
            </td>
        </tr>
        <tr>
            <td valign="top" width="123" style="">
                <p><span>Additional data</span></p>
            </td>
        </tr>
    </tbody>
</table>'''

soup = BeautifulSoup(html_doc, 'html.parser')

soup.td['colspan'] = 4
print(soup.prettify())

Prints:

<table border="0" cellpadding="0" cellspacing="0" width="704">
 <tbody>
  <tr>
   <td colspan="4" style="" valign="top" width="704">
    <p>
     <span>
      Short description
     </span>
    </p>
   </td>
  </tr>
  <tr>
   <td style="" valign="top" width="123">
    <p>
     <span>
      Additional data
     </span>
    </p>
   </td>
  </tr>
 </tbody>
</table>

Leave a Reply