Parsing HTML [ ] output

Viewed 20

Good morning, I am trying to extract from the website https://shop.fattoriaterranova.it/it/14-marmellate the price and cost of each jam jar.

This is my code:

#import modules
import urllib.request, urllib.parse, urllib.error
from urllib import request
from bs4 import BeautifulSoup
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

#BeautifulSoup & url
url = 'https://shop.fattoriaterranova.it/it/14-marmellate'
html = request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html,"html.parser")

results = soup.find(id='product_list')
products = results.find_all('ul', class_='product_list grid row')

print(products)

for product in products:
    price_elem = product.find('span', class_='price product-price')
    prod_elem = product.find('a', class_='product-name')
    if None in (price_elem, prod_elm):
        continue
    print(price_elem.strip())
    print(prod_elem.strip())
    print(results.strip())

The output I get is

[ ]

What am I doing wrong?

Thank you

1 Answers

You are searching for the same class inside the class you just found.

results = soup.find(id='product_list')
products = results.find_all('ul', class_='product_list grid row')

products contains all the child elements of product_list, so searching for product_list again in here will return no results. Instead you should search for the li elements which containt the price and product info

#import modules
import urllib.request, urllib.parse, urllib.error
from urllib import request
from bs4 import BeautifulSoup
import ssl

# Ignore SSL certificate errors
ctx = ssl.create_default_context()
ctx.check_hostname = False
ctx.verify_mode = ssl.CERT_NONE

#BeautifulSoup & url
url = 'https://shop.fattoriaterranova.it/it/14-marmellate'
html = request.urlopen(url, context=ctx).read()
soup = BeautifulSoup(html,"html.parser")
results = soup.find(id='product_list')
products = results.find_all('li', class_='ajax_block_product')

for product in products:
    price_elem = product.find('span', class_='price product-price').string
    prod_elem = product.find('a', class_='product-name').string
    if price_elem and prod_elem:
        print(f"{prod_elem}: {price_elem}")

OUTPUT

Marmellata di Limoni 212 ml :  3,50 € 
Marmellata di Limoni 314 ml :  4,00 € 
Marmellata di Arance 212 ml :  3,50 € 
Marmellata di Mandarini :  4,50 € 
Marmellata di Arance 106 ml :  2,70 € 
Marmellata di Arance 314 ml :  4,00 € 
Marmellata di Pesche 314 ml :  4,50 € 
Marmellata di Arance Amare 212 ml :  3,50 € 
Marmellata di Albicocche 314 ml :  4,50 € 
Marmellata di Amarene 212 ml :  6,00 € 
Marmellata di Limoni 106 ml :  2,70 € 
Marmellata di Fichi 212 ml :  5,00 € 
Confettura di Peperoncini piccanti :  4,00 € 
Marmellata di Limoni & Zenzero 212 ml :  4,00 € 
Related