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

I am having a problem with Beautiful Soup and I cannot figure it out for the life of me. I am attempting to find an element and get the text inside it.

location = t.find('span', itemprop='name').get_text()

But I keep getting this error:

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

However, if I run location = t.find('span', itemprop='name') I get the proper Tag object: <class 'bs4.element.Tag'> location=<span itemprop="name">Hobart, IN</span>

What would cause this error?

Here is the full code:

with open('source.html') as f:
   soup = BeautifulSoup(f.read(), "html.parser")

# Extract script and style elements
for s in soup(["script", "style"]):
    s.extract()

tr = soup.find_all("tr")
for t in tr:
    location = t.find('span', itemprop='name').get_text() # ERROR

Source HTML is a table:

                <tr class="espresso-table-row live">
            <td class="event_title event-61492">Title of class</td>
                            <td class="venue_title event-61492"><span itemprop="name">Location</span></td>
                        <td class="start_date event-61492" data-value="1645452000">
                <ul class="ee-table-view-datetime-list">
                                                <li class="datetime-id-630">
                                February 21, 2022                           </li>
                                    </ul>
            </td>
            <td class="td-group reg-col" nowrap="nowrap"><a id="a_register_link-61492" class="a_register_link" href="https://website.com/">Register</a></td>
        </tr>


Solution 1:[1]

Question do not provide detailed information about the html, but it looks like there are <tr> selected that do not have any <span> with that attribute.

You should select your tags more specific for example with css selectors:

for span in soup.select('tr span[itemprop="name"]'):
    location = span.get_text()

Example

This html will lead with your code to an error cause your find_all('tr') is selecting both <tr> but in the second one you won't find() a <span> - result is None and None has no attribute get_text(). It will give you the location if you select it more specific.

from bs4 import BeautifulSoup
html = '''
<tr class="espresso-table-row live">
    <td class="event_title event-61492">Title of class</td>
    <td class="venue_title event-61492"><span itemprop="name">Location</span></td>
    <td class="start_date event-61492" data-value="1645452000">
        <ul class="ee-table-view-datetime-list">
            <li class="datetime-id-630">
                February 21, 2022
            </li>
        </ul>
    </td>
    <td class="td-group reg-col" nowrap="nowrap"><a id="a_register_link-61492" class="a_register_link" href="https://website.com/">Register</a></td>
</tr>
<tr>
    any other content
</tr>
'''

soup = BeautifulSoup(html)

for span in soup.select('tr span[itemprop="name"]'):
    location = span.get_text()

print(location)

Output

Location

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1