Search code examples
pythonpython-3.xbeautifulsoupnonetype

'NoneType' object has no attribute 'text' can't get it working


I have some code to get the new products from a supplier. Well I just started the code. But now I get a NoneType on the following line: voorraad = result.find('span', {'class': 'po_blok_stoc'}).text.

It is the same as the other classes so it should be working, right?

this is the full code:

# import libraries
from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup

# specify the url
url = "https://www.erotischegroothandel.nl/nieuweproducten/"

# Connect to the website and return the html to the variable ‘page’
uClient = uReq(url)
page_html = uClient.read()
uClient.close()

# parse the html using beautiful soup and store in variable `soup`
soup = BeautifulSoup(page_html, 'html.parser')
results = soup.find_all('div', {'class': 'po_blok'})

records = []
for result in results:
    titel = result.find('span', {'class': 'po_blok_titl'}).text
    staat = result.find('span', {'class': 'po_blok_nieu'}).text
    voorraad = result.find('span', {'class': 'po_blok_stoc'}).text

    records.append((titel, staat, voorraad))

print(records)

This is the html were I get the info from:

<div class="po_blok">
            <a href="/aanmelden" class="po_blok_crea">Klant worden</a>
            <a href="/login.html" class="po_blok_logi">Al klant? Klik hier om in te loggen</a>
            <a href="/massage/massageolie-siliconen/nuru_play_body2body_massage_gel__4l_40589.html">
                <img src="https://cdn.edc.nl/250/NGR04000R.jpg" alt="productnaam">
                <span class="po_blok_nieu">Nieuw</span>
                <span class="po_blok_titl">Nuru Play Body2Body Massage Gel – 4L</span>
                <span class="po_blok_stoc">Voorradig</span>
            </a>
        </div> 

Solution

  • The reason is that many of those elements are None. The error is for those elements, so we handle it ..

    # import libraries
    from urllib.request import urlopen as uReq
    from bs4 import BeautifulSoup
    
    # specify the url
    url = "https://www.erotischegroothandel.nl/nieuweproducten/"
    
    # Connect to the website and return the html to the variable ‘page’
    uClient = uReq(url)
    page_html = uClient.read()
    uClient.close()
    
    # parse the html using beautiful soup and store in variable `soup`
    soup = BeautifulSoup(page_html, 'html.parser')
    results = soup.find_all('div', {'class': 'po_blok'})
    
    records = []
    for result in results:
        titel = result.find('span', {'class': 'po_blok_titl'}).text
        staat = result.find('span', {'class': 'po_blok_nieu'}).text
        voorraad = result.find('span', {'class': 'po_blok_stoc'})
        if voorraad:
            records.append((titel, staat, voorraad.text))
        
    
    for record in records:
        print(record)
    

    Output:-

    ('Nuru Play Body2Body Massage Gel – 4L', 'Nieuw', 'Voorradig')
    ('Nuru Play Body2Body Massage Gel – 335 ml', 'Nieuw', 'Voorradig')
    ('Nuru Glow Body2Body Massage Gel – 335 ml', 'Nieuw', 'Voorradig')
    ('P-Trigasm Prostaat Vibrator met Roterende Kralen', 'Nieuw', 'Voorradig')
    ('Gaia Eco Vibrator - Roze', 'Nieuw', 'Voorradig')
    ('Gaia Eco Vibrator - Blauw', 'Nieuw', 'Voorradig')
    ('Zachte Gladde Anaal Dildo', 'Nieuw', 'Voorradig')  .etc