Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

<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>
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading