html = '<p class="product-new-price">96<sup>33</sup> <span class="tether-target tether-enabled tether-element-attached-top tether-element-attached-left tether-target-attached-top tether-target-attached-right">Lei</span>
</p>'

soup = BeautifulSoup(html, 'html.parser')

sup_elem = soup.find("sup").string # 33 - it works

¿Cómo obtengo el "96" antes del elemento?

0
Ancyent 2 oct. 2019 a las 14:36

3 respuestas

La mejor respuesta

Puede tomar la etiqueta anterior Hermano

from bs4 import BeautifulSoup

html = '''<p class="product-new-price">96<sup>33</sup> <span class="tether-target tether-enabled tether-element-attached-top tether-element-attached-left tether-target-attached-top tether-target-attached-right">Lei</span>
</p>'''

soup = BeautifulSoup(html, 'html.parser')

elem1 = soup.find("sup").previousSibling
elem2 = soup.find("sup").text # 33 - it works

print ('.'.join([elem1, elem2]))

Salida:

96.33
1
chitown88 2 oct. 2019 a las 11:55

Use select en su lugar.

from bs4 import BeautifulSoup

html = '''<p class="product-new-price">96<sup>33</sup> <span class="tether-target tether-enabled tether-element-attached-top tether-element-attached-left tether-target-attached-top tether-target-attached-right">Lei</span>
</p>'''

soup = BeautifulSoup(html, 'html.parser')
print(soup.select_one('.product-new-price').text.strip().replace('Lei',''))

No hay "." en origen pero siempre puedes dividir por 100

print(int(soup.select_one('.product-new-price').text.strip().replace('Lei',''))/100)
1
QHarr 2 oct. 2019 a las 12:28

Puedes usar el método de niños. Devolverá una lista de todos los elementos secundarios de la etiqueta p. (6 será el primer hijo de él.

html = '<p class="product-new-price">96<sup>33</sup> <span class="tether-target tether-enabled tether-element-attached-top tether-element-attached-left tether-target-attached-top tether-target-attached-right">Lei</span>
</p>'

soup = BeautifulSoup(html, 'html.parser')

elem = list(soup.find("p").children)[0] #0th element of the list will be 96
sup_elem = soup.find("sup").string

result = elem + '.' + sup_elem #96.33

1
Swaroop Deval 2 oct. 2019 a las 11:51