Agustin
Agustin

Reputation: 5

How to get text in BeautifulSoup?

I'm trying to get the string "Carlos Pellegrini 551, C1009ABK Buenos Aires, Argentina" using BS but I'm having problems. (It's just before the at the end)

<span class="
hp_address_subtitle
js-hp_address_subtitle
jq_tooltip
" rel="14" data-source="top_link" data-coords="," data-component="tooltip" data-tooltip-text="
<p>Ubicación <strong>excelente</strong>, ¡puntuada con 9,3/10! <small>(puntaje basado en <strong>Más recomendado</strong> comentarios)</small></p>
<p>Valorado por las personas <strong>después de hospedarse en</strong> el Hotel Panamericano Buenos Aires.</p>
" data-tooltip-animation="false" tabindex="0" data-bbox="-58.4028009366937,-34.6199039159457,-58.3590682554297,-34.5839730827995" data-node_tt_id="location_score_tooltip" data-width="350" title="" aria-describedby="tooltip-1">
Carlos Pellegrini 551, C1009ABK Buenos Aires, Argentina
</span>

I tried the following:

soup.find(attrs={'class':"hotel_address_subtitle"}).get_text()

But I get None ad a result.

Please help me!

Upvotes: 0

Views: 207

Answers (2)

MendelG
MendelG

Reputation: 20018

You can do this to only find text on the website:

soup.find_all(text=True)

instead of:

soup.find(attrs={'class':"hotel_address_subtitle"}).get_text()

try:

for element in soup.find_all(text=True):
    print(element)

edit after I saw your comment, try this:

from bs4 import BeautifulSoup
import requests

page = requests.get("https://www.booking.com/hotel/ar/panamericano-buenos-aires.es-ar.html")
soup = BeautifulSoup(page.content, 'html.parser')
output = soup.find("span", {"class": "hp_address_subtitle"})
print(output.text)

outputs

Carlos Pellegrini 551, C1009ABK Buenos Aires, Argentina

Upvotes: 1

Humayun Ahmad Rajib
Humayun Ahmad Rajib

Reputation: 1560

You can use "select_one". you can try it:

from bs4 import BeautifulSoup
html_doc='''<span class="
hp_address_subtitle
js-hp_address_subtitle
jq_tooltip
" rel="14" data-source="top_link" data-coords="," data-component="tooltip" data-tooltip-text="
<p>Ubicación <strong>excelente</strong>, ¡puntuada con 9,3/10! <small>(puntaje basado en <strong>Más recomendado</strong> comentarios)</small></p>
<p>Valorado por las personas <strong>después de hospedarse en</strong> el Hotel Panamericano Buenos Aires.</p>
" data-tooltip-animation="false" tabindex="0" data-bbox="-58.4028009366937,-34.6199039159457,-58.3590682554297,-34.5839730827995" data-node_tt_id="location_score_tooltip" data-width="350" title="" aria-describedby="tooltip-1">
Carlos Pellegrini 551, C1009ABK Buenos Aires, Argentina
</span>'''

soup = BeautifulSoup(html_doc, 'lxml')
result = soup.select_one("span").text

print(result)

Output will be:

Carlos Pellegrini 551, C1009ABK Buenos Aires, Argentina

Upvotes: 0

Related Questions