如何用美体组合找课

2024-06-16 10:26:02 发布

您现在位置:Python中文网/ 问答频道 /正文

<hknbody>
        <tr>
            <td class="padding_25 font_7 bold xicolor_07" style="width:30%">

                date

            </td>
            <td class="font_34 xicolor_42">

                19 Eylül 2013

            </td>
        </tr>
        <tr>
            <td style="height:10px" colspan="3"></td>
        </tr>
        <tr>
            <td class="bgcolor_09" style="height:5px" colspan="3"></td>
        </tr>
        <tr>
            <td style="height:10px" colspan="3"></td>
        </tr>
        <tr>
            <td class="padding_25 font_7 bold xicolor_07" style="width:30%">

                Size

            </td>
            <td class="font_34 xicolor_42">
               650 cm

类名相同,类在同一个表中。 如何找到正确的数据?示例:如果<td class="padding_25 font_7 bold xicolor_07>中不存在“date”,则不需要提取日期并查找下一个数据。在


Tags: 数据datestylewidthtrclasstdfont
1条回答
网友
1楼 · 发布于 2024-06-16 10:26:02

如果这是您的HTML,并且您可以对其进行更改,那么您应该使用semantic HTML来用class、id或name属性标记元素,这些属性描述数据的含义,而不是其外观。然后您将有一个明确的方法来选择所需的标记。在

因为你只需要这样做:

import re
from bs4 import BeautifulSoup

soup = BeautifulSoup(html)

date_tag = soup.find('td', text=re.compile('^\s*date\s*$'))    # find first <td> containing text "date"
if date_tag:
    date_value = date_tag.find_next_sibling('td').text.strip()

>>> print date_value
19 Eylül 2013

相关问题 更多 >