Python - BeautifulSoup: find td width
<p>I have many tables and each has table data tag something like this:</p>

<pre><code>&lt;td width="563" valign="top" bgcolor="#FFFF99" class="text"&gt;
...
&lt;td width="12" bgcolor="#FFFF99" class="lettnav"&gt;
&lt;td bgcolor="#FFFF99" class="lettnav"&gt;
</code></pre>

<p>The goal is to locate which <code>&lt;td&gt;</code> with the highest value. To do that, first I want to get the value of the width using beautifulsoap (if no with just print empty string).</p>

<p>Here is my code snippet that so far did not work:</p>

<pre><code>soup = BeautifulSoup(page, 'html.parser')
cells = soup.findAll("td",{"width": re.compile('\d')})

for aCell in cells:
   width=aCell.find("width")
   print(width)
</code></pre>

<p>Any help?</p>

<p>In BeautifulSoup attributes are accessed in dict notation (see <a href="https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes" rel="nofollow">https://www.crummy.com/software/BeautifulSoup/bs4/doc/#attributes</a> for more information).</p>

<p>Using <code>width=aCell["width"]</code> your code works:</p>

<pre><code>for aCell in cells:
    width=aCell["width"]
    print(width)
</code></pre>

<p>If you're just interested in the max value you could also omit the <code>for</code> loop and use a list comprehension instead:</p>

<pre><code>maxwidth = max(int(x["width"]) for x in cells)
</code></pre>

python html beautifulsoup