Fabian
Fabian

Reputation: 9

Python: BeautifulSoup - Accessing a element without further specification

How do I get the text of that bold element? Thanks in advance

<tr>
    <td>
        <div class="graph-legend-color" 
        style="width:12px;height:11px;background- 
        color:#3366CC">
        </div>
    </td>
    <td class="percent">48,9 %</td>
    <td class="number">92.234</td>
    **<td>Proxy-Block Types From Download Media Type Blocklist</td>**
</tr>

Upvotes: 0

Views: 43

Answers (1)

Andrej Kesely
Andrej Kesely

Reputation: 195553

As you see, the bold element (<td>) is last <td> element inside the <tr> tag. So you select all <td> tags inside <tr> tag and get the element with index -1 (in Python that means last index):

data = """
<tr>
    <td>
        <div class="graph-legend-color"
        style="width:12px;height:11px;background-
        color:#3366CC">
        </div>
    </td>
    <td class="percent">48,9 %</td>
    <td class="number">92.234</td>
    <td>Proxy-Block Types From Download Media Type Blocklist</td>
</tr>"""

from bs4 import BeautifulSoup

soup = BeautifulSoup(data, 'lxml')

print(soup.select('tr > td')[-1].text)

Prints:

Proxy-Block Types From Download Media Type Blocklist

Upvotes: 1

Related Questions