How do you extract the floats from the elements in a python list?
<p>I am using BeautifulSoup4 to build a script that does financial calculations. I have successfully extracted data to a list, but only need the float numbers from the elements.</p>

<p>For Example:</p>

<pre><code>Volume = soup.find_all('td', {'class':'text-success'})

print (Volume)
</code></pre>

<p>This gives me the list output of:</p>

<pre><code>[&lt;td class="text-success"&gt;+1.3 LTC&lt;/td&gt;, &lt;td class="text- success"&gt;+5.49&lt;span class="muteds"&gt;340788&lt;/span&gt; LTC&lt;/td&gt;, &lt;td class="text-success"&gt;+1.3 LTC&lt;/td&gt;,]
</code></pre>

<p>I want it to become:</p>

<pre><code>[1.3, 5.49, 1.3]
</code></pre>

<p>How can I do this?</p>

<p>Thank-you so much for reading my post I greatly appreciate any help I can get.</p>

<p>You can find the first text node inside every <code>td</code>, split it by space, get the first item and convert it to <code>float</code> via <code>float()</code> - the <code>+</code> would be handled automatically:</p>

<pre><code>from bs4 import BeautifulSoup

data = """
&lt;table&gt;
    &lt;tr&gt;
        &lt;td class="text-success"&gt;+1.3 LTC&lt;/td&gt;
        &lt;td class="text-success"&gt;+5.49&lt;span class="muteds"&gt;340788&lt;/span&gt; LTC&lt;/td&gt;
        &lt;td class="text-success"&gt;+1.3 LTC&lt;/td&gt;
    &lt;/tr&gt;
&lt;/table&gt;"""

soup = BeautifulSoup(data, "html.parser")

print([
    float(td.find(text=True).split(" ", 1)[0])
    for td in soup.find_all('td', {'class':'text-success'})
])
</code></pre>

<p>Prints <code>[1.3, 5.49, 1.3]</code>.</p>

<p>Note how the <code>find(text=True)</code> helps to avoid extracting the <code>340788</code> in the second <code>td</code>.</p>

python regex beautifulsoup