2

i'm currently working on scraping this website:

https://listado.mercadolibre.com.ve/oficinas-suministros-papeleria-papel-en-aragua/resma-carta-alpes

i've been successful until i apply the "text" attribute to the decimal price variable.

this is the code i've been using:

from urllib.request import urlopen as uReq
from bs4 import BeautifulSoup as soup

my_url = 'https://listado.mercadolibre.com.ve/oficinas-suministros-papeleria-papel-en-aragua/resma-carta-alpes'

uClient = uReq(my_url)
page_html = uClient.read()
uClient.close()

page_soup = soup(page_html, "html.parser")
containers = page_soup.findAll("div", {"class": "item__info-container"})
for container in containers:
    title_container = container.a.text.strip()
    price_container_fraction = container.find("span" ,{"class":"price__fraction"}).text
    price_container_decimal = container.find("span" ,{"class":"price__decimals"})
    print(title_container)
    print(price_container_fraction)
    print(price_container_decimal)

this is the html ad block:

<div class="item__info-container ">
    <div class="item__info item--hide-right-col ">
        <h2 class="item__title list-view-item-title"> <a class="item__info-title" href="https://articulo.mercadolibre.com.ve/MLV-523616759-resma-tamano-carta-marca-alpes-caja-_JM"> <span class="main-title"> Resma Tamaño Carta Marca Alpes Caja </span> </a></h2>
        <div class="price__container">
            <div class="item__price "> <span class="price__symbol">Bs.</span> <span class="price__fraction">4</span> <span class="price__decimals">50</span></div>
        </div>
        <div class="item__stack_column">
            <div class="item__stack_column__info item__only-status">
                <div class="stack_column_item status">
                    <div class="item__status">
                        <div class="item__condition"> 2 vendidos </div>
                    </div>
                </div>
            </div>
        </div>
        <div class="stack_colum_right without-attributes without-reviews">
            <div class="stack_column_right__bottom "> </div>
        </div>
    </div>
</div>

This is the result:

enter image description here

i need to gather the 2 elements of the price and then add them but no luck. any help?

1
  • 1
    Can you post expected output? Commented Sep 27, 2018 at 12:47

2 Answers 2

2

The problem seems to be that the price_decimals span isn't always populated, find returns None when this occurs. I've added the following to your code and tested it, it produces what I've guessed is your required output. The if conditional checks for None if that's True it sets price_container_decimal to 00, if it's not it sets it to the text property value of the span.

price_container_decimal = container.find("span" ,{"class":"price__decimals"})    
if price_container_decimal == None:
    price_container_decimal = "00"
else:
    price_container_decimal = container.find("span" ,{"class":"price__decimals"}).text
Sign up to request clarification or add additional context in comments.

Comments

1

price_container_decimal is printing the entire html content, add .text to print the decimal value only.

price_container_decimal = container.find("span" ,{"class":"price__decimals"}).text

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.