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>To find the <em>td</em> with the largest width you can use <em>max</em> on the list of <em>td's</em> returned from the <em>find_all</em> call, setting the key to <code>key=lambda t: int(t["width"])</code>:</p>

<pre><code>soup = BeautifulSoup(page, 'html.parser')
cells = soup.find_all("td", width=True)

mx_td = max(cells, key=lambda t: int(t["width"]))
</code></pre>

<p><code>t["width"]</code> accesses the <em>attribute</em> value, we need to call <em>int</em> on the result or the value would be compared <a href="https://en.wikipedia.org/wiki/Lexicographical_order" rel="nofollow">lexicographically</a> i.e <code>2 &gt; 100</code> would be True.</p>

python html beautifulsoup