Access Tag via beautifulsoup

Asked

Viewed 33 times

0

Hello, I’m having difficulty accessing the price that is in the third line of the code via beautifulsoup.

Does anyone have any idea how to access?

    <span id="ctl00_Conteudo_ctl01_spanPrecoPor" class="for">
        <span id="ctl00_Conteudo_ctl01_lblPrecoPor">Por:</span>
        <strong id="ctl00_Conteudo_ctl01_precoPorValue">R$<i class="sale price">6.099,00</i></strong>

        <span id="ctl00_Conteudo_ctl01_comDescontoPlus" class="discountTxtPlus"></span>
    </span>
    <span id="ctl00_Conteudo_ctl01_spanParcel" class="parcel">ou até <strong>12x</strong> de <strong> R$508,25</strong> sem juros</span> 
    <span id="ctl00_Conteudo_ctl01_spanSave" class="save">
        <span id="ctl00_Conteudo_ctl01_lblEconomize"></span>
        <strong id="ctl00_Conteudo_ctl01_valorEconomize"></strong>
    </span>

    <span id="ctl00_Conteudo_ctl01_spanPrecoFull" class="for full">
        <span id="ctl00_Conteudo_ctl01_lblPrecoFull"></span>
        <strong id="ctl00_Conteudo_ctl01_precoFullValue"></strong>
    </span>

    <span id="ctl00_Conteudo_ctl01_spanParcelFull"></span>
    <span id="ctl00_Conteudo_ctl01_spanSaveFull" class="save full" style="display: none">
        <span id="ctl00_Conteudo_ctl01_lblEconomizeFull"></span>
        <strong id="ctl00_Conteudo_ctl01_valorEconomizeFull"></strong>
    </span>


</span>

I am using the function below that brings the name but not the price:

def lista_dados(url):

browser = webdriver.Chrome()
urlDado = url
browser.get(urlDado)
innerHTML = browser.execute_script("return document.body.innerHTML")
browser.close()

soupDados = BeautifulSoup(innerHTML, 'lxml')

produtoNome = soupDados.b.string
produtoPreco = soupDados.find('span', {'strong':'id'})

d = {
    'Nome Produto':produtoNome,
    'Preco':produtoPreco,
    'teste':'teste'
    }
#print(soupDados.prettify())
print(d)

return d
  • 1

    Hello Diogo, what have you tried? What is the difficulty you are encountering? You could post the part of your code where you try to access this tag?

  • Hello Fernando. I am using the function below with the correct name but the price is not:

1 answer

0

Just get the class sale price and pick up your content

# -*- coding: utf-8 -*-
from bs4 import BeautifulSoup

data = """<span id="ctl00_Conteudo_ctl01_spanPrecoPor" class="for">
        <span id="ctl00_Conteudo_ctl01_lblPrecoPor">Por:</span>
        <strong id="ctl00_Conteudo_ctl01_precoPorValue">R$<i class="sale price">6.099,00</i></strong>

        <span id="ctl00_Conteudo_ctl01_comDescontoPlus" class="discountTxtPlus"></span>
    </span>
    <span id="ctl00_Conteudo_ctl01_spanParcel" class="parcel">ou até <strong>12x</strong> de <strong> R$508,25</strong> sem juros</span> 
    <span id="ctl00_Conteudo_ctl01_spanSave" class="save">
        <span id="ctl00_Conteudo_ctl01_lblEconomize"></span>
        <strong id="ctl00_Conteudo_ctl01_valorEconomize"></strong>
    </span>

    <span id="ctl00_Conteudo_ctl01_spanPrecoFull" class="for full">
        <span id="ctl00_Conteudo_ctl01_lblPrecoFull"></span>
        <strong id="ctl00_Conteudo_ctl01_precoFullValue"></strong>
    </span>

    <span id="ctl00_Conteudo_ctl01_spanParcelFull"></span>
    <span id="ctl00_Conteudo_ctl01_spanSaveFull" class="save full" style="display: none">
        <span id="ctl00_Conteudo_ctl01_lblEconomizeFull"></span>
        <strong id="ctl00_Conteudo_ctl01_valorEconomizeFull"></strong>
    </span>   
</span>"""

soup = BeautifulSoup(data)

div = soup.find('i', class_='sale price')

print div.text

Upshot

6.099,00

Browser other questions tagged

You are not signed in. Login or sign up in order to post.