Beautiful Soup and Pandas extract number

Viewed 35

I have this structure:

<tr id="table3620_0_5" class="l1">
            <td class="r">       North America</td>
            <td x:num="02/12/20">02/12/20</td>
            <td x:num="" class="r">5553226</td>
            <td x:num="" class="r">TEST TWI</td>
            <td x:num="0.03365930063626542">3.37 %</td>
            <td/>
            <td x:num="0.03365930063626542">3.37 %</td>
         </tr>

With pandas read html I can extract the table but I'm interested in x:num value instead of the value of the tag. I'm trying to explore a solution with Beautiful Soup as well, but I came up with nothing as of now

1 Answers

You can try beautifulsoup

from bs4 import BeautifulSoup
import re

s = """<tr id="table3620_0_5" class="l1">
            <td class="r">       North America</td>
            <td x:num="02/12/20">02/12/20</td>
            <td x:num="" class="r">5553226</td>
            <td x:num="" class="r">TEST TWI</td>
            <td x:num="0.03365930063626542">3.37 %</td>
            <td/>
            <td x:num="0.03365930063626542">3.37 %</td>
         </tr>"""

soup = BeautifulSoup(s, 'lxml')

output = [i.get('x:num') for i in soup.findAll("td", {"x:num" : True})]

print(output)
['02/12/20', '', '', '0.03365930063626542', '0.03365930063626542']

output[-2:]
['0.03365930063626542', '0.03365930063626542']
Related