Felipe
Felipe

Reputation: 3

Web Scraping - Get elements from HTML using the Class

I have the following HTML

<div class="ui_columns is-gapless is-mobile">
    <div class="ui_column is-4 providerLogoOuter">
        <span class="providerLogoInner" title=""><imgsrc="https://static.tacdn.com/img2/branding/hotels/Hoteiscom_384x164.png" class="providerImg" alt="Hoteis.com">

But I need to get only the "Hoteis.com" from the alt=

I am trying to get it using BeautifulSoap, but how can I get this element?

name_player = soup.find_all(class_='providerLogoInner')[0]

It Returns no elements

Upvotes: 0

Views: 52

Answers (2)

Bitto
Bitto

Reputation: 8225

Is that malformed html or a typo?

html="""
<div class="ui_columns is-gapless is-mobile">
<div class="ui_column is-4 providerLogoOuter">
<span class="providerLogoInner" title=""><imgsrc="https://static.tacdn.com/img2/branding/hotels/Hoteiscom_384x164.png" class="providerImg" alt="Hoteis.com">
"""
from bs4 import BeautifulSoup
soup=BeautifulSoup(html,'html5lib')
print(soup.find(class_='providerImg')['alt'])

Output:

Hoteis.com

Upvotes: 1

Prayson W. Daniel
Prayson W. Daniel

Reputation: 15578

You can do:

from bs4 import BeautifulSoup


raw = '''
<div class="ui_columns is-gapless is-mobile">
    <div class="ui_column is-4 providerLogoOuter">
        <span class="providerLogoInner" title=""><imgsrc="https://static.tacdn.com/img2/branding/hotels/Hoteiscom_384x164.png" class="providerImg" alt="Hoteis.com">
'''

soup = BeautifulSoup(raw,'html5lib')

hotel_lnk = soup.find('span',{'class':'providerLogoInner'}).next['alt']

print(hotel_lnk)

#'Hoteis.com'

Upvotes: 1

Related Questions