Как извлечь текст из span, если нет class?
Буду благодарен за подсказку о том, как извлечь текст из тега span, если нет class.
Код:
<div class="product-tile__wrapper product-tile__wrapper--name">
<div class="product-tile-name">
<p class="product-tile-name__text">
<span data-at-product-tile-brand="" class="product-tile-name__text--brand">
DOLCE&GABBANA </span>
<br>
<span data-at-product-tile-title=""> Набор Light Blue Eau De Toilette
</span>
</p>
</div>
</div>
Пробую:
brand_names = product.find('span', class_='product-tile-name__text--brand').get_text()
product_names = product.find()
brand_names извлекается отлично, а вот с product_names - проблема...
Ответы (2 шт):
Автор решения: Сергей Ш
→ Ссылка
from bs4 import BeautifulSoup
h = '''<div class="product-tile__wrapper product-tile__wrapper--name">
<div class="product-tile-name">
<p class="product-tile-name__text">
<span data-at-product-tile-brand="" class="product-tile-name__text--brand">
DOLCE&GABBANA </span>
<br>
<span data-at-product-tile-title=""> Набор Light Blue Eau De Toilette
</span>
</p>
</div>
</div>
'''
soup = BeautifulSoup(h, 'html.parser')
brand, title = [x.text.strip() for x in soup.find_all('span')]
print(brand, title, sep='\n')
brand, title = soup.find('p').stripped_strings
print(brand, title, sep='\n')
DOLCE&GABBANA
Набор Light Blue Eau De Toilette
Автор решения: alex
→ Ссылка
Помогло очевидное:
product_name = product.find('p', class_='product-tile-name__text')
.find_all('span')[1].text