Beautifulsoup + HTML...how to ignore a few h3 classes

Viewed 230

I got the following code:

from bs4 import BeautifulSoup
import requests
import re

source = requests.get('https://tienda.mimo.com.ar/mimo/junior/ropa-para-ninas.html').text

soup = BeautifulSoup(source, 'lxml')

for name_product, old_price, special_price in zip(soup.select('h3', class_='titprod'), 
                                                  soup.select('span[id^="old-price"]'),
                                                  soup.select('span[id^="product-price"]')):
    print(f'Name: {name_product.text.strip()} |  Old price = {old_price.text.strip()} | Discounted price = {special_price.text.strip()}')

that outputs:

Name: Para acceder a la promoción seleccione el banco y la tarjeta de crédito que corresponda |  Old price = $ 295 | Discounted price = $ 236
Name: ¡Gracias por suscribirte al newsletter! |  Old price = $ 990 | Discounted price = $ 743
Name: Elegí por talle |  Old price = $ 2.300 | Discounted price = $ 1.725
Name: TAPABOCAS |  Old price = $ 1.550 | Discounted price = $ 1.163
Name: REMERA JR TOWN |  Old price = $ 2.990 | Discounted price = $ 2.243
Name: CAMISOLA NENA DELFI |  Old price = $ 1.990 | Discounted price = $ 1.493

As you can see instead of taking the proper Product's names. Its actually taking what it says on the header on the very first two lines (Name: Para acceder a la promoción seleccione el banco y la tarjeta de crédito que corresponda...... ) , which are using the same css_selector (titprod). Don't know how to get deep into the LI class (rounded black square) to get to the proper name of the product (rounded red square). Due to this the list is misplaced, causing the prices to not correspond to the name of the product on each line.

enter image description here

2 Answers

I had to change

soup.select('h3', class_='titprod'),

for

soup.select('h3.titprod')

as @AndrejKessely suggested on the comments and it works perfectly

The problem is that you are using select() method wrong way. To select all <h3> with class=titprod, you need to write soup.select('h3.titprod').

The class_= parameter belongs to .find() and .find_all() functions (that don't take CSS selectors).

Link to .find_all() in the bs4 documentation.

Examples of CSS selectors from the bs4 documentation.

Related