Python:BeautifulSoup-在没有进一步说明的情况下访问元素

时间:2018-07-31 11:05:15

标签: python beautifulsoup

如何获取该粗体元素的文本? 预先感谢

<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>

1 个答案:

答案 0 :(得分:1)

如您所见,粗体元素(<td><td>标签内的最后<tr>元素。因此,您选择了<td>标记内的所有<tr>标记,并获得了索引为-1的元素(在Python中表示最后一个索引):

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)

打印:

Proxy-Block Types From Download Media Type Blocklist