Not getting data after index 5 when using beatifulsoup to scrape

Question:

This is my code.

from bs4 import BeautifulSoup
import requests
import time
import mysql.connector


header = {
    "user-agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36" ,
    'referer':'https://www.google.com/'
}

htmlRequest = requests.get("https://www.promodescuentos.com/",headers=header).text
soup = BeautifulSoup(htmlRequest, 'lxml')
promos = soup.find_all('article', class_ = 'thread cept-thread-item thread--type-list imgFrame-container--scale thread--deal')
for index, promo in enumerate(promos):

    promo_id = str.lstrip(promo['id'], 'thread_')
    promo_name = promo.find('a', class_ = 'cept-tt thread-link linkPlain thread-title--list js-thread-title')['title']
    promo_prize = promo.find('span', class_ = 'thread-price text--b cept-tp size--all-l size--fromW3-xl')
    original_prize = promo.find('span', class_ = 'mute--text text--lineThrough size--all-l size--fromW3-xl')
    promo_points = promo.find("span", {"class": "cept-vote-temp vote-temp vote-temp--burn"})
    if promo_prize is not None:
        text_prize = str.lstrip(promo_prize.text, '$')
    else:
        text_prize = 'Not available'

    if original_prize is not None:
        text_discount = str.lstrip(original_prize.text, '$')
    else:
        text_discount = 'Not available'

    if promo_points is not None:
        text_points = promo_points.text.strip()[:-1]
    else:
        text_points = 'Not available'

    print(f"Index: {index}")
    print(f"Promo Id: {promo_id}")
    print(f"Titulo Promo: {promo_name}")
    print(f"Precio Promo: {text_prize}")
    print(f"Descuento Promo: {text_discount}")
    print(f"Puntos: {text_points}")
    print('')


The output is the following

Index: 0
Promo Id: 797731
Titulo Promo: Santa clara: 2x1 en Helados de 1L del 5 al 31 de Diciembre
Precio Promo: 1
Descuento Promo: Not available
Puntos: Not available

Index: 1
Promo Id: 797661
Titulo Promo: Amazon: Apple iPhone 11 Pro 256 GB Color Verde(Midnight Green)(Reacondicionado)
Precio Promo: 9,641
Descuento Promo: Not available
Puntos: 1209

Index: 2
Promo Id: 797725
Titulo Promo: Sonics VPN: 1 Año de Cuenta Premium (Windows, Android e iOS)
Precio Promo: GRATIS
Descuento Promo: 966
Puntos: 967

Index: 3
Promo Id: 797693
Titulo Promo: Costco: Apple iPad Mini 8.3" Wi-Fi 64GB Rosa, precio aplicando el cupon y pagando con Paypal (12 MSI)
Precio Promo: 8,999
Descuento Promo: Not available
Puntos: 1043

Index: 4
Promo Id: 797702
Titulo Promo: Tarjeta de Crédito sin Anualidad de por Vida + 2 meses de Amazon Prime Gratis (Solo trae tu Nómina a Banorte)
Precio Promo: Not available
Descuento Promo: Not available
Puntos: 1760

Index: 5
Promo Id: 797750
Titulo Promo: Amazon: Apple Watch Series 7 (GPS, 41 mm) caja de aluminio verde, con correa deportiva Clover, regular (renovado)
Precio Promo: 5,842
Descuento Promo: Not available
Puntos: 1049

Index: 6
Promo Id: 797716
Titulo Promo: Elektra: Batería Xiaomi (REDMI) Mi Power Bank 3 Pro 20000MAh Negra
Precio Promo: 
Descuento Promo: Not available
Puntos: 

Index: 7
Promo Id: 795242
Titulo Promo: Renos de Fuego 2022
Precio Promo: Not available
Descuento Promo: Not available
Puntos: 

Index: 8
Promo Id: 797526
Titulo Promo: Beerhouse 24 Pack Modelo Premium ¡Envío Gratis!
Precio Promo: 
Descuento Promo: 
Puntos: 

So after index 5 my data from promo_prize, original_prize, promo_points are not showing up.
I did a test by getting all html text and put it on a file .html and it works fine but when using a html request to website i got this problem. Notice that promo_id and promo_name i got no problems in any index, thats because im getting the attribute value. The other ones im getting the text element.

Any ideas?

Answers:

You can try using their Ajax API to load the product HTML:

import requests
from bs4 import BeautifulSoup

url = "https://www.promodescuentos.com/?page={page}&ajax=true&layout=horizontal"

headers = {
    "User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:107.0) Gecko/20100101 Firefox/107.0"
}

for page in range(1, 3):  # <-- increase number of pages here.
    data = requests.get(url.format(page=page), headers=headers).json()
    soup = BeautifulSoup(data["data"]["content"], "html.parser")

    for article in soup.select("article"):
        title = article.select_one(".thread-title").text
        id_ = article["id"].split("_")[-1]
        price = article.select_one(".thread-price")
        price = price.text if price else "N/A"

        puntos = article.select_one(".cept-vote-temp")
        puntos = puntos.get_text(strip=True) if puntos else "N/A"

        print("{:<10} {:<10} {:<10} {}".format(id_, price, puntos, title))

Prints:

798010     N/A        458°       RAPPI: STARBUCKS DIVERSOS PAQUETES O COMBOS AL 70% DE DESCUENTO
797772     -$500      853°       Costco: $500 de descuento comprando en la app pagando con Paypal
797731     $1         684°       Santa clara: 2x1 en Helados de 1L del 5 al 31 de Diciembre
797661     $9,641     1496°      Amazon: Apple iPhone 11 Pro 256 GB Color Verde(Midnight Green)(Reacondicionado)
797725     GRATIS     1153°      Sonics VPN: 1 Año de Cuenta Premium (Windows, Android e iOS)
797693     $8,999     N/A        Costco: Apple iPad Mini 8.3" Wi-Fi 64GB Rosa, precio aplicando el cupon y pagando con Paypal (12 MSI)
797702     N/A        2023°      Tarjeta de Crédito sin Anualidad de por Vida + 2 meses de Amazon Prime Gratis (Solo trae tu Nómina a Banorte)
797750     $5,842     N/A        Amazon: Apple Watch Series 7 (GPS, 41 mm) caja de aluminio verde, con correa deportiva Clover, regular (renovado)
797716     $599       1415°      Elektra: Batería Xiaomi (REDMI) Mi Power Bank 3 Pro 20000MAh Negra
795242     N/A        12229°     Renos de Fuego 2022
797526     $292       1727°      Beerhouse 24 Pack Modelo Premium ¡Envío Gratis!
797510     N/A        1877°      BBVA 100% de tu compra en puntos del 7 al 11 de diciembre (usuarios seleccionados)


...and so on.
Answered By: Andrej Kesely