0

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> 
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
  • `for result in results` - so you will do this many times, looking inside many `
    `s. Maybe some of them contain all these ``s, and others do not?
    – Karl Knechtel Nov 30 '20 at 08:02

1 Answers1

1

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
Abhishek Rai
  • 2,159
  • 3
  • 18
  • 38
  • Hi Abhishek Rai When i use your option, i get only one result back, this is the last item that it can find. 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)) Gives as result: ('Sensuva Ultra Dik Glijmiddel Op Waterbasis - Naturel', 'Nieuw', 'Voorradig') – Tim Bronsgeest Nov 30 '20 at 06:59
  • I have added the output..Are you getting a different output than mine? – Abhishek Rai Nov 30 '20 at 07:19
  • Yea i get only one result. and that is the last item in the product list on the website. I'm not getting all products as you shown. – Tim Bronsgeest Nov 30 '20 at 07:55
  • @TimBronsgeest Have posted the entire code..try it. – Abhishek Rai Nov 30 '20 at 08:00
  • OMG i had not used `for record in records` Thank you so much for your help! – Tim Bronsgeest Nov 30 '20 at 08:14