无法从SPAN标记获取文本

2024-04-27 03:52:50 发布

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

我试图解析的网站结构如下所示:

<table border="0" cellpadding="3" cellspacing="0" width="100%">
    <tr height="25">
        <td class="th" style="border:none" width="2%"> </td>
        <td class="th">movie</td>
        <td class="th"> </td>
        <td class="th"> </td>
    </tr>

    <tr id="place_1">
        <td style="color: #555; vertical-align: top; padding: 6px">
            <a name="1"></a>1.
        </td>

        <td style="height: 27px; vertical-align: middle; padding: 6px 30px 6px 0">
            <a class="all" href="/326/">MOVIE TITLE IN SPANISH</a>

            <br/>

            <span class="text-grey">MOVIE TITLE IN ENGLISH</span> 
        </td>

        <td style="width: 85px">
            <div style="width: 85px; position: relative">
                <a class="continue" href="/326/votes/">
                    9.191
                </a> 

                <span style="color: #777">
                    (592 184)
                </span>
            </div>
        </td>
    </tr>

    ...
    ...
    ...

问题是我无法在span标记中获取文本。我试过.text至于a-tag,也试过.get\text()。但这些都不管用。我的Python代码:

for row in table.find_all('tr')[1:]:

    info = row.find_all('td')

    movies.append({
        'spn_title' : info[1].a.text,
        'eng_title' : info[1].span.text,
    })

我得到的错误:

AttributeError: 'NoneType' object has no attribute 'get_text'

或者

'eng_title' : info[1].span.text AttributeError: 'NoneType' object has no attribute 'text'


Tags: textinfotitlestyletableallwidthtr
2条回答

请尝试以下操作。另外,检查soup变量,因为我可以毫无问题地运行代码。我怀疑在HTML后面的某个地方,你没有一个这样的出现在一行中。你知道吗


如果类名是一致的,则只能筛选具有相应类型元素的限定行类。使用bs4.7.1。你知道吗

for row in table.select('tr :has(span.text-grey):has(a.all)'):
    movies.append({
        'spn_title' : row.select_one('.all').text,
        'eng_title' : row.select_one('.text-grey').text
    })
print(movies)

否则,您需要一种方法来处理不存在的情况。例如

for row in table.find_all('tr')[1:]:
     movies.append({
        'spn_title' : row.select_one('.all').text if row.select_one('.all') is not None else 'None',
        'eng_title' : row.select_one('.text-grey').text if row.select_one('.text-grey') is not None else 'None'
    })
print(movies)

我认为你应该用innerHTML。你知道吗

info[1].getElementsByTagName('span')[0].innerHTML 

应该有用。你知道吗

相关问题 更多 >