Fjott
Fjott

Reputation: 1137

Beautifulsoup get specific instance of class

first time using beautifulsoup. Trying to scrape a value from a website with the following structure:

<div class="overview">
<i class="fa fa-instagram"></i>
<div class="overflow-h">
<small>Value #1 here</small>
<small>131,390,555</small>
<div class="progress progress-u progress-xxs">
<div style="width: 13%" aria-valuemax="100" aria-valuemin="0" aria-valuenow="92" role="progressbar" class="progress-bar progress-bar-u">
</div>
</div>
</div>
</div>

<div class="overview">
<i class="fa fa-facebook"></i>
<div class="overflow-h">
<small>Value #2 here</small>
<small>555</small>
<div class="progress progress-u progress-xxs">
<div style="width: 13%" aria-valuemax="100" aria-valuemin="0" aria-valuenow="92" role="progressbar" class="progress-bar progress-bar-u">
</div>
</div>
</div>
</div>

I want the second <small>131,390,555</small> in the first <div class="overview"></div>

This is the code I am trying to use:

# Get the hashtag popularity and add it to a dictionary
for hashtag in hashtags:

    popularity = []

    url = ('http://url.com/hashtag/'+hashtag)
    r = requests.get(url, headers=headers)

    if (r.status_code == 200):
        soup = BeautifulSoup(r.content, 'html5lib') 
        overview = soup.findAll('div', attrs={"class":"overview"})
        print overview

        for small in overview:
                popularity.append(int(small.findAll('small')[1].text.replace(',','')))


        if popularity:
            raw[hashtag] = popularity[0]
            #print popularity[0]
            print raw


        time.sleep(2)

    else:
        continue

The code works as long as the second <small>-value is populated in both div-overviews. I really only need the second small-value from the first overview-div. I have tried to get it like this:

overview = soup.findAll('div', attrs={"class":"overview"})[0]

But I only get this error:

    self.__class__.__name__, attr))
AttributeError: 'NavigableString' object has no attribute 'findAll'

Also is there somehow to not "break" the script if the is no small-value at all? (Have the script just replace the empty value with an zero, and continue)

Upvotes: 0

Views: 108

Answers (2)

uingtea
uingtea

Reputation: 6554

you can use index but I suggest to use CSS selector and nth-child()

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

# only get first result
small = soup.select_one('.overview small:nth-child(2)')
print(small.text.replace(',',''))

# all results
secondSmall = soup.select('.overview small:nth-child(2)')
for small in secondSmall:
    popularity.append(int(small.text.replace(',','')))

print(popularity)

enter image description here

Upvotes: 1

Nishith Savla
Nishith Savla

Reputation: 327

If you just want the 2nd small tag in the 1st div only, this will work:

soup = BeautifulSoup(r.content, 'html.parser')
overview = soup.findAll('div', class_ = 'overview')
small_tag_2 = overview[0].findAll('small')[1]
print(small_tag_2)

If you want the 2nd small tag in every overview div, iterate using the loop:

soup = BeautifulSoup(r.content, 'html.parser')
overview = soup.findAll('div', class_ = 'overview')
for div in overview:
    small_tag_2 = div.findAll('small')[1]
    print(small_tag_2)

Note: I used html.parser instead of html5lib. If you know how to work with html5lib, then it's your choice.

Upvotes: 1

Related Questions